Found 33577 Articles for Programming

How to generate random strings with upper case letters and digits in Python?

Rajendra Dharmkar
Updated on 10-Aug-2023 21:06:48

1K+ Views

You generate random strings with upper case letters and digits in Python. Here are three different ways to do it: Using random.choices() and string.ascii_uppercase In this method, we will use the random.choices() function to randomly choose characters from the string.ascii_uppercase and string.digits strings to generate a random string. Example We first import the random and string modules, which we will use to generate random characters and create strings, respectively. We define the length of the random string we want to generate (in this example, it's 10). We define the possible characters that can be used to create the random string, ... Read More

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

Yaswanth Varma
Updated on 16-Apr-2025 17:34:46

32K+ Views

In Python Variables are just the labels that are pointing to the values. They don't carry any built-in metadata about their names. That’s why there is no direct built-in method to get the variable name as a string. However, we can achieve this, by inspecting the environment such as using the globals(), locals(), or the inspect module. Let's dive into the article to learn more about getting a variable name as a string. Using Python globals() Function The Python globals() function is used to return a dictionary representing the current global symbol table. It provides ... Read More

How to convert hex string into int in Python?

Yaswanth Varma
Updated on 16-Apr-2025 17:40:11

48K+ Views

A Hexadecimal is the base-16 number system that uses the digits from the '0 to 9' and letters from 'A to F'. It is commonly used in the computers to represent the binary data in the readable format. In this article we are going to learn about converting hex string into int. For example, if we receive the input as a hex string("12xf2") and need to convert it into a integer. For achieving this, Python provides the multiple methods let's explore them one by one. Using Python int() Function The Python int() function is used to convert ... Read More

How to remove specific characters from a string in Python?

SaiKrishna Tavva
Updated on 19-Mar-2025 11:57:29

2K+ Views

When working with strings in Python, you might often find the need to remove specific characters. This could be driven by various needs such as data cleaning, formatting, or simply modifying text for output. Below are several methods to remove specific characters from a string. Using str.replace() Using a Loop Using str.translate() Using List Comprehension Using 'str.replace()' Method The simplest way to remove specific characters from a string is to use the str.replace() method. This method allows you to replace occurrences of ... Read More

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

561 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

455 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

282 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

Advertisements