Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 790 of 855
How to generate random strings with upper case letters and digits in Python?
Generating random strings with uppercase letters and digits is common in Python for creating passwords, tokens, or unique identifiers. Here are several effective methods to accomplish this ? Using random.choices() The random.choices() function allows repeated character selection, making it ideal for generating random strings of any length ? import random import string # Define the length and character set length = 10 characters = string.ascii_uppercase + string.digits # Generate random string with possible duplicates result = ''.join(random.choices(characters, k=length)) print(result) 8KM2N7QX5R Using random.choice() with Loop This method builds the ...
Read MoreHow to get a variable name as a string in Python?
In Python, variables are just labels pointing to values. They don't carry any built-in metadata about their names, so there's no direct method to get a variable name as a string. However, we can achieve this by inspecting the environment using globals(), locals(), or the inspect module. Let's explore these approaches ? Using globals() Function The globals() function returns a dictionary representing the current global symbol table. We can search through this dictionary to find variable names by their values. Syntax globals() Example Here's how to find a global variable name ...
Read MoreHow to convert hex string into int in Python?
A Hexadecimal is the base-16 number system that uses the digits from '0 to 9' and letters from 'A to F'. It is commonly used in computers to represent binary data in a readable format. In this article, we will learn about converting hex strings into integers. For example, if we receive input as a hex string ("fe00") and need to convert it into an integer, Python provides multiple methods. Let's explore them one by one. Using int() Function The int() function is the most common method to convert a hex string to an integer. It accepts ...
Read MoreHow to remove specific characters from a string in Python?
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 a ...
Read MoreHow to extract numbers from a string in Python?
Python provides several methods to extract numbers from strings. Whether you need integers, floating-point numbers, or both, these techniques will help you parse numeric data from text efficiently. Using Regular Expressions (re.findall) The most powerful approach uses regular expressions to match different number patterns ? import re # Extract integers and floats text = "The price is $29.99 and quantity is 5" numbers = re.findall(r'\d+\.\d+|\d+', text) print("String format:", numbers) # Convert to appropriate numeric types numeric_values = [] for num in numbers: if '.' in num: ...
Read MoreDoes Python have a string \'contains\' substring method?
Python provides several methods to check if a string contains a substring. The most common and Pythonic approach is using the in operator, which returns True if the substring exists and False otherwise. Using the 'in' Operator The in operator is the simplest way to check for substring containment ? text = "Python is a powerful programming language" # Check if substring exists if "Python" in text: print("Substring found!") else: print("Substring not found.") Substring found! Case-Sensitive vs Case-Insensitive Search The in ...
Read MoreWhat does the \'b\' character do in front of a string literal in Python?
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 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 received in ...
Read MoreHow to parse a string to float or int in python?
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 (e.g., 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 ...
Read MoreHow to check if a string contains only decimal characters?
In many programming scenarios, it is common to validate user input to ensure it contains the specified value. For example, when accepting age, pin-code or numbers from the user, we make sure the input entered is completely digits. One specific way to do this is by checking if the string contains only decimal characters. Decimal characters are the characters used to write base-10 numbers (the digits from 0-9). Python provides built-in methods to perform this check easily. Let's dive into the article to learn more about it. Using isdecimal() Method The Python isdecimal() method is used to ...
Read MoreHow to get a string left padded with zeros in Python?
String padding is commonly needed when formatting numbers, creating fixed-width output, or aligning text. Python provides several built-in methods to left-pad strings with zeros: rjust(), format(), zfill(), and f-string formatting. Using rjust() Method The rjust() method right-justifies a string by padding it with a specified character on the left. It takes two parameters: the total width and the fill character (default is space). Syntax string.rjust(width, fillchar) Example text = "Welcome to Tutorialspoint" padded = text.rjust(30, '0') print("Original string:", text) print("Padded string:", padded) print("Length:", len(padded)) Original string: Welcome ...
Read More