Trim a String from Both Sides in Python

Tapas Kumar Ghosh
Updated on 01-Jun-2023 15:55:58

1K+ Views

Python has inbuild functions like lstrip(), strip(), and rstrip() that can be used to remove the character from both sides. The term trim the string of characters means to remove the character from the given string. For example − Given string is ‘iiiiiiiiiiiBOXERiiiiiiiiii’ and the character ‘i’ from both sides. The final result becomes BOXER. Syntax The following syntax is used in the examples − strip() This is a predefined method used in Python to trim the character from both sides of the string. [1:] [:-1] [1:] − This represents the after-slicing on the left side to ... Read More

Format Time in AM/PM Format in Python

Tapas Kumar Ghosh
Updated on 01-Jun-2023 15:54:19

13K+ Views

In Python, we have some time built-in functions like strftime() and datetime.now() that can be used to find the time in AM/PM format. The format time in AM/PM format uses various applications such as user interface, report & document, data visualization, and event scheduling. We will speak the time of AM when timing between midnight 11:59:00 to 12 noon. In the same way, we can say that the time difference between 12 noon to 11:59:00 midnight is called PM. The abbreviation AM/PM is used to indicate the exact time. Syntax The following syntax is used in the example &miinus; strftime('%I:%M:%S ... Read More

Convert String to Array of Characters in Python

Tapas Kumar Ghosh
Updated on 01-Jun-2023 15:53:01

9K+ Views

In this article, we will learn how to develop a Python program using append(), extend(), and list() that will convert the given string into an array of characters. In this program, the array of characters represents the group of individual characters that break from the string. Syntax The following syntax is used in the examples − append() This is a predefined method used in Python that adds the character from the end. extend() This method is used to break the characters from a string. list() The method converts the string into a list of individual characters. [*string_variable_name] ... Read More

Convert Local Time to GMT in Python

Tapas Kumar Ghosh
Updated on 01-Jun-2023 15:51:25

7K+ Views

When we are creating a web service that allows users all over the world to book events, we might use this program to convert each user's local time to GMT before putting it in our database. This would make comparing and displaying event times easier for users in different time zones comparing and displaying event times easier for users in different time zones. In Python, we have some time built-in functions like timezone(), localize(), now(), and astimezone() that can be used to convert the local time into GMT. The local time represents the current time whereas GMT is defined by ... Read More

Trim a String from the Right Side in Python

Tapas Kumar Ghosh
Updated on 01-Jun-2023 15:48:14

621 Views

In Python, we have a predefined function rstrip() to remove the character from the right side. It means it will remove the whitespace from the rightside side of the string. Let’s take an example to understand the trim of a string from the left side. In the given string “WIRELESS” remove the right string LESS and get the result value as “WIRE”. In the given string “kingdom” remove the right string dom and get the result value as “king”. Syntax The following syntax used in the examples are − isspace() This is a predefined method used in ... Read More

Convert Array of Characters to String in Python

Tapas Kumar Ghosh
Updated on 01-Jun-2023 15:40:08

10K+ Views

In Python, we have some built-in functions like join(), map(), reduce(), and len() that can be used to convert the array of characters into the string. The array of characters is defined by breaking each character from the string. Python has a predefined method join() to join all the characters to make the string. Another way to solve the program is by using list comprehension and the join() method. Let’s take an example to understand the conversion of an array of characters into a string − The character is ‘p’, ‘e’, and ‘n’: The string becomes a pen. The ... Read More

Print All Patterns Matching Given Pattern from a File in Python

Tapas Kumar Ghosh
Updated on 01-Jun-2023 15:34:47

292 Views

Finding lines in a file that match a particular pattern is a typical operation with many applications, such as log analysis, text processing, and data filtering. In this article, we will discuss the Python Program to Print all the Pattern that Matches Given Pattern From a File. To solve this problem, we will first create a pattern in a file to save it. Our task is to programmatically create the exact pattern that we see in the file. By applying some conditions it will check whether the pattern matches or not from a given file. Syntax with open("file_name.txt",  "r") as file The ... Read More

Python Program to Demonstrate Time Arithmetic

Tapas Kumar Ghosh
Updated on 01-Jun-2023 15:31:36

220 Views

The process of conducting mathematical operations on time values is referred to as time arithmetic. Adding or subtracting time durations, determining the difference between two-time values, or determining the average of a group of time values are a few examples of time arithmetic. Some of the applications are used to demonstrate time arithmetic such as time tracker, data analysis, and time scheduling. In Python, we have time built-in functions i.e. strptime() and divmod() that can be used to demonstrate the time arithmetic. There are two ways of time formats that are 12 hours and 24 hours. Let’s take an example ... Read More

Convert Milliseconds to Minutes and Seconds in Python

Tapas Kumar Ghosh
Updated on 01-Jun-2023 15:30:10

6K+ Views

In Python, we have a built-in function int(), timedelta(), and divmod() that can be used to get the number in the form integer and is useful for converting Miliseconds to Minutes and seconds. The milliseconds are defined by the short duration of time. The milliseconds are equal to one-thousandth of the second. When 5000 milliseconds convert into minutes the result value is 0.08 minutes and 5 seconds. For example- The photographer clicks the picture and saves it in a gallery which takes some seconds that time is preferred to be milliseconds. Syntax int() The int() function accepts the ... Read More

Trim a String from the Left Side in Python

Tapas Kumar Ghosh
Updated on 01-Jun-2023 15:28:46

407 Views

Python has various internal functions like isspace(), lstrip(), and replace() are used for trimming a given string. It means it will remove the whitespace from the left side of the string. Let’s take an example to understand the trim of a string from the left side. In the given string “MICROBOX” remove the left string MI and get the result value as “CROBOX”. In the given string “synapse” remove the left string syn and get the result value as “apse”. In this article, we will learn how to develop a Python program that will trim the string from ... Read More

Advertisements