Found 10534 Articles for Python

How to extract numbers from a string in Python?

Rajendra Dharmkar
Updated on 10-Aug-2023 20:54:45

12K+ Views

There are several ways to extract numbers from a string in Python. One way to extract numbers from a string is to use regular expressions. Regular expressions are patterns that you can use to match and manipulate strings. Here's an example code snippet that uses regular expressions to extract all the numbers from a string: Using the re.findall() function Example In this example, we're using the re.findall() function to search for all occurrences of numbers in the text string. The regular expression \d+\.\d+|\d+ matches both floating-point numbers and integers. import re text = "The price of the book is $29.99" ... Read More

Does Python have a string 'contains' substring method?

Rajendra Dharmkar
Updated on 10-Aug-2023 12:02:36

501 Views

In Python, a substring is a sequence of characters that appears within a larger string. For example, in the string "Hello, world!", the substring "world" appears within the larger string. A substring can be a single character, or it can be a longer sequence of characters. Substrings are useful when you need to extract or manipulate specific parts of a string. You can also check if a substring is contained within a larger string using Python's string methods. Python provides a built-in method to check if a string contains a substring. The method is called ‘in’ and it returns a ... Read More

How to convert byte literals to python strings?

Rajendra Dharmkar
Updated on 13-Feb-2020 10:32:41

437 Views

To convert byte literals to Python strings, you need to decode the bytes. It can be done using the decode method on the bytes object.  example>>> b"abcde".decode("utf-8") u'abcde'You can also map bytes to chr if the bytes represent ASCII encoding as follows −bytes = [112, 52, 52] print("".join(map(chr, bytes)))Outputp44

What does the \'b\' character do in front of a string literal in Python?

Yaswanth Varma
Updated on 15-Apr-2025 17:41:59

11K+ Views

A string is a collection of characters that can represent a single word or a complete phrase. Since you can directly assign strings in Python to a literal (unlike other technologies) it is easy to use them. The string literals are typically enclosed in quotes (' ' or " ") and they represent a sequences of characters. However, in some scenarios we will encounter a string that starts with a lowercase b for example,  b'WELCOME', Where the b prefix stands for bytes literal. Bytes literals are useful when dealing with binary data, where data is transmitted or ... Read More

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

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

252 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

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 Convert Lowercase Letters in String to Uppercase in Python?

Yaswanth Varma
Updated on 14-Apr-2025 11:30:55

3K+ Views

Converting the lowercase letters in a string to uppercase is a common task in Python. When working with the user input or formatting output, Python provides efficient ways to handle the letter casing. The most commonly used method is the built-in string method upper(). We can use loops and list comprehensions with this method, if we need to convert individual tokens of a string to upper case. Using python upper() Method The Python upper() method is used to convert all the lowercase characters present in a string into uppercase. However, if there are elements in the string that are already ... Read More

Advertisements