Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 136 of 855
Python Program to remove the last specified character from the string
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 MorePython Program to trim a string from both sides
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 MorePython Program to Format time in AM-PM format
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 MorePython program to convert local time into GMT
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 MorePython Program to convert the array of characters into the string
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 MorePython Program to Print all the Pattern that Matches Given Pattern From a File
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 MorePython Program to demonstrate the time arithmetic
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 MorePython Program to Convert Milliseconds to Minutes and Seconds
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 MorePython Program to Read and printing all files from a zip file
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 MorePython Program to Count number of lines present in the file
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