Python Articles - Page 920 of 929

How to parse a string to float or int in python?

SaiKrishna Tavva
Updated on 06-Mar-2025 17:17:31

450 Views

Python provides built-in functions to convert strings into numerical data types like integers and floats. However, it's crucial to handle errors that may arise when a string cannot be properly converted (eg, trying to convert "abc" to an integer). Following are several methods to parse a string to float or int. Using int() and float() with Error Handling The most straightforward approach is using the int() and float() functions directly within a try-except block to catch ValueError exceptions. Example In the following example, the try block attempts to convert the string. If the conversion fails, a ValueError is raised. The ... Read More

How to strip down all the punctuation from a string in Python?

Yaswanth Varma
Updated on 16-Apr-2025 17:37:48

2K+ Views

When working with the text data in Python, We will come across the situations where we find the unwanted punctuation marks (like ., :?!) to remove. Removing punctuation helps to normalize text, making it easier to analyze. Python provides the several ways to achieve this, Let's dive into the article to learn ore about it. Using Python translate() Method The Python str.translate() method is a sequel for the maketrans() method in the string module. This method uses the translation table generated by the maketrans() and translate all characters based on the one-to-one mapping in the said table. ... Read More

How to check if a string contains only decimal characters?

Yaswanth Varma
Updated on 14-Apr-2025 11:46:15

5K+ Views

In many programming scenarios, it is common to validate the user input to ensure it contains the specified value. For example, when accepting age, pin-code or numbers form the user, we make sure the input is entered is completely digits. One specific way to do this is by checking if the string contains only decimal numbers. Decimal characters are the characters used to write base-10 numbers (the digits from 0-9). Python provides the built-in methods to perform this check easily, Let's dive into the article to learn more about it. Using Python isdecimal() Method The Python isdecimal() ... Read More

How to get a string left padded with zeros in Python?

Tarun Chandra
Updated on 19-Oct-2022 12:10:52

18K+ Views

A string is a group of characters that can represent a single word or a whole sentence. In Python there is no need to declare strings explicitly we can directly assign them to a literal. Therefore, easy to use them. A string is an object of the String class, which has multiple methods to manipulate and access strings. In this article, we are going to find out how to get a string left padded with zeros in Python. Using the rjust() function One way to achieve this is using the built-in string function named rjust(). The width argument is the ... Read More

Mobile

How to translate a python string according a given dictionary?

Yaswanth Varma
Updated on 14-Apr-2025 11:43:53

2K+ Views

In Python, translating the string using a dictionary is used for text manipulation, encoding. It is typically achieved by using the str.translate() method along with the str.maketrans() to define the character mappings. The dictionary passed to the maketrans() contains key-value pairs where the keys are characters to replace and values are their corresponding replacements. Let's dive into the article to learn more about translating a string using the dictionary. Using Python str.translate() Method The Python str.translate() method is a sequel for the maketrans() method in the string module. This method uses the translation table generated by the ... Read More

How to invert case for all letters in a string in Python?

Tarun Chandra
Updated on 19-Oct-2022 12:02:47

3K+ Views

A string is a group of characters that can represent a single word or a complete sentence. In Python there is no need to declare variables explicitly using the datatype specifier you can directly assign them to a literal. Therefore, compared to other technologies like Java it is easy to use Python strings. For manipulating and accessing strings, Python includes several built in functions and methods. You can find these functions in a class named String. In this article, we are going to focus on how to invert the case for all letters in a string in Python. Using the ... Read More

How to split at NEWLINEs in Python?

Tarun Chandra
Updated on 07-Dec-2022 07:31:08

32K+ Views

In this article, we are going to find out how to split at newlines in Python. In general, split means to divide or to make a group of objects divide into smaller groups. The first approach to split at NEWLINES in python is by using the inbuilt method splitlines(). It takes a multi-line string as input and it returns a separated string that is divided at the new line. It doesn’t take any parameters. The splitlines() method is an inbuilt string method and it is used only for splitting at the new lines. Example 1 In the program given below, ... Read More

How to split string by a delimiter string in Python?

Yaswanth Varma
Updated on 11-Apr-2025 12:03:31

2K+ Views

While working with the strings in python, we will find the situations to break the string into parts. This is called splitting a string and is usually done based on a delimiter, this is nothing but a character or sequence of characters that separate the parts of the string. Python provides the several ways for achieving this, depending on whether the delimiter is simple string or a pattern. Let's dive into the article to learn more about splitting string by delimiter. Using python split() Method The python split() method is used to split the words in a string separated by ... Read More

How to remove all trailing whitespace of string in Python?

Rajendra Dharmkar
Updated on 02-May-2023 12:33:55

4K+ Views

Any character or group of characters that depicts horizontal or vertical space is known as whitespace. A whitespace character usually takes up space on a page even though it does not correspond to any visible mark when it is rendered. Pressing the spacebar will allow you to enter a whitespace character. On many keyboards, the Tab key can also be used to enter horizontal whitespace, although the length of the space may vary. Any spaces or tabs that follow the final non-whitespace character on the line until the newline are considered trailing whitespace. Whitespace in Python Whitespace is a type ... Read More

How to search a string in backwards direction in Python?

Tarun Chandra
Updated on 07-Dec-2022 07:05:30

4K+ Views

In this article, we are going to find out how to search a string in backward direction in Python. The first method is to use the inbuilt python String class's rindex() method. The highest index of any substring in the given string is returned by the Python string rindex() method. By highest index, we mean that if a given substring appears twice or three times in a string, the rindex() method will return the index of the substring's rightmost or final occurrence. The main disadvantage of this function is that it throws an exception if the string contains no substrings. ... Read More

Advertisements