Found 10784 Articles for Python

How to get a variable name as a string in Python?

Tarun Chandra
Updated on 27-Aug-2023 12:23:18

29K+ Views

A string is a collection of characters stored as a single value. Unlike other technologies there is no need to explicitly declare strings in Python (for that matter any variable), you just need to assign strings to a literal this makes Python strings easy to use. In Python, a string is represented by the class named String. This class provides several functions and methods using which you can perform various operations on strings. In this article, we are going to focus on how to get a variable name as a string in Python. Variables are essentially name mappings to objects, ... Read More

How to convert hex string into int in Python?

Tarun Chandra
Updated on 24-Aug-2023 16:29:26

42K+ Views

A string is a group of characters that can be used to represent a single word or an entire phrase. Strings are simple to use in Python since they do not require explicit declaration and may be defined with or without a specifier. In Python, the class named string represents the strings. This class provides several built-in methods, using these methods we can perform various operations on strings. In this article, we are going to find out how to convert hex string into an integer in Python. Using the int() method One way to achieve this is using the inbuilt ... Read More

How to remove specific characters from a string in Python?

Rajendra Dharmkar
Updated on 11-Aug-2023 10:43:59

1K+ Views

Here, we are considering some code examples to remove specific characters from a string in Python: Using str.replace() In this method, we will use the str.replace() method to replace the specific characters with an empty string. Example We define a string called my_string that contains the original string we want to modify. We use the str.replace() method to replace all occurrences of the character 'o' with an empty string, effectively removing them from the string. We store the resulting string in a new variable called result. We print the resulting string. # define the string my_string = "Lorem Ipsum!" # ... Read More

How to extract numbers from a string in Python?

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

5K+ 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

358 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

380 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?

Tarun Chandra
Updated on 25-Oct-2022 14:32:05

10K+ 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. Python includes a number of built in functions and methods to perform various operations on strings, a string is an object of the String class, which contains these methods. In this article, we are going to find out what is the b character that is present in front of a string literal does in python. The b literal in front of the string ... Read More

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

Rajendra Dharmkar
Updated on 30-Sep-2019 06:50:30

176 Views

To parse a string to int, you can use the following:try:     print int('112') except ValueError:     print 'Cannot parse'This will give you the output:112To parse a string to float, you can use the following:try:     print float('112.15') except ValueError:     print 'Cannot parse'This will give you the output:112.15

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

Rajendra Dharmkar
Updated on 11-Aug-2023 11:06:59

902 Views

Stripping down all the punctuation from a string in Python can be achieved using various methods. Here are some examples: Using string.punctuation Example The string.punctuation constant contains all the ASCII punctuation characters. We can use this constant to remove all the punctuation characters from the string. We are using a list comprehension to iterate over each character in the string and check if it's not in the string.punctuation constant. If it's not a punctuation character, we add it to the no_punct variable. import string # sample string with punctuation text = "Lorem, Ipsum!!!" # remove punctuation no_punct = "".join(char for ... Read More

How to check if a string contains only decimal characters?

Tarun Chandra
Updated on 19-Oct-2022 12:25:45

4K+ Views

A string is a group of characters that may be used to represent a single word or an entire sentence. Strings are simple to use in Python since they do not require explicit declaration and may be defined with or without a specifier. In Python strings are represented by the class named string and this class provides several built-in methods to manipulate and access strings. In this article, we are focusing on how to check if a string contains only decimal characters in Python. Using the isdigit() function One way to achieve this is using the inbuilt string function isdigit(). ... Read More

Advertisements