Python Articles

Page 136 of 855

Python Program to remove the last specified character from the string

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 1K+ Views

Removing the last specified character from a string is a common text manipulation task in Python. This operation is useful for data cleaning, input validation, and string formatting. Python provides several approaches including slicing, rstrip(), and string replacement methods. Method 1: Using String Slicing String slicing with [:-1] removes the last character by excluding it from the slice ? text = "Tutorialspoints" result = text[:-1] print("Original string:", text) print("After removing last character:", result) Original string: Tutorialspoints After removing last character: Tutorialspoint Method 2: Using rstrip() for Specific Character The rstrip() ...

Read More

Python Program to trim a string from both sides

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 1K+ Views

Python provides several built-in methods to trim or remove characters from both sides of a string. The term "trim" means to remove unwanted characters from the beginning and end of a string. For example − Given string is 'iiiiiiiiiiiBOXERiiiiiiiiii' and we want to remove the character 'i' from both sides. The final result becomes BOXER. Methods for Trimming Strings Using strip() Method The strip() method removes whitespace or specified characters from both ends of a string ? s_trim = " APPLE " trim_str = s_trim.strip() print("Trim the whitespace from ...

Read More

Python Program to Format time in AM-PM format

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 13K+ Views

In Python, we can format time in AM/PM (12-hour) format using built-in functions like strftime() and datetime.now(). The AM/PM format is commonly used in user interfaces, reports, documents, data visualization, and event scheduling. AM represents time from midnight (00:00) to 11:59, while PM represents time from noon (12:00) to 11:59 PM. Syntax The basic syntax for formatting time in AM/PM format is − strftime('%I:%M:%S %p') The strftime() function uses format codes to represent different time components: %I − Hours in 12-hour format (01-12) %M − Minutes (00-59) %S − Seconds (00-59) %p ...

Read More

Python program to convert local time into GMT

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 7K+ Views

When building web applications that serve users across different time zones, converting local time to GMT (Greenwich Mean Time) is essential. Python provides several built-in modules like datetime, pytz, and time to handle timezone conversions. GMT is also known as UTC (Coordinated Universal Time) and serves as the global time standard. Key Methods for Time Conversion datetime.now() Returns the current local time without timezone information ? pytz.timezone() Creates a timezone object for a specific region or country. Requires the pytz module ? localize() Attaches timezone information to a naive datetime object ? astimezone() Converts a ...

Read More

Python Program to convert the array of characters into the string

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 10K+ Views

In Python, converting an array of characters into a string is a common task that can be accomplished using several built-in methods. An array of characters is simply a list where each element is a single character. Python provides multiple approaches including join(), map(), reduce(), and loops to combine these characters into a single string. Let's understand this with examples ? Characters ['p', 'e', 'n'] become the string "pen" Characters ['S', 'A', 'N', 'D', 'B', 'O', 'X'] become the string "SANDBOX" Using join() Method The join() method is the most efficient and Pythonic way ...

Read More

Python Program to Print all the Pattern that Matches Given Pattern From a File

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 384 Views

Finding lines in a file that match a particular pattern is a common operation with many applications, such as log analysis, text processing, and data filtering. In this article, we will discuss how to create a Python program that prints all patterns matching a given pattern from a file. Syntax with open("file_name.txt", "r") as file: content = file.read() The open() function works with the 'with' statement to open the file safely. The open function accepts two parameters: the filename and the mode ('r' for reading). Algorithm The following steps ...

Read More

Python Program to demonstrate the time arithmetic

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 311 Views

Time arithmetic involves performing mathematical operations on time values such as calculating durations, finding differences between times, or adding time intervals. This is essential for applications like scheduling, data analysis, and time tracking. Python provides built-in functions like strptime() and divmod() to handle time arithmetic operations with both 12-hour and 24-hour formats. Understanding Time Formats Python supports two main time formats ? 12-hour format: Uses AM/PM notation (e.g., 2:30:15 PM) 24-hour format: Uses 24-hour notation (e.g., 14:30:15) Syntax For 12-hour format ? strptime('Time_format_in_12_hours', '%I:%M:%S %p') Format specifiers for 12-hour ...

Read More

Python Program to Convert Milliseconds to Minutes and Seconds

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 6K+ Views

In Python, milliseconds can be converted to minutes and seconds using built-in functions like int(), timedelta(), and divmod(). Milliseconds represent short durations of time, where 1000 milliseconds equals 1 second. For example, 5000 milliseconds converts to 0 minutes and 5 seconds, or 125000 milliseconds converts to 2 minutes and 5 seconds. Basic Conversion Formula The conversion follows this logic: Total seconds = milliseconds ÷ 1000 Minutes = total seconds ÷ 60 Remaining seconds = total seconds % 60 Method 1: Using Integer Division This approach uses floor division and modulo operators to extract ...

Read More

Python Program to Read and printing all files from a zip file

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 7K+ Views

Multiple files are compressed together and stored in a single file format called a zip file. There are applications such as file management, data processing, file conversion, and backup & archive of files. In Python, we have built-in functions like namelist() and infolist() that can be used to read and print all files from a zip file. archive.zip 📁 file1.txt 📁 file2.txt 📁 file3.txt Python ...

Read More

Python Program to Count number of lines present in the file

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 10K+ Views

In Python, we can count the number of lines in a file using several built-in functions and methods. This is useful for analyzing file contents or processing large text files. We'll explore different approaches to count lines efficiently. Syntax The basic syntax for opening a file in Python is ? with open("filename.txt", mode) as file: # file operations The open() function accepts two main parameters ? filename.txt ? The name of the file to open. mode ? Determines how the file is opened ('r' for reading, 'w' for ...

Read More
Showing 1351–1360 of 8,546 articles
« Prev 1 134 135 136 137 138 855 Next »
Advertisements