In Python, you can use the import statement to use functions or variables defined in another module. Python provides several ways to import multiple modules, each with its own advantages for different scenarios. Basic Module Import Suppose you have two modules module1.py and module2.py that contain some functions − # module1.py def say_hello(name): print("Hello, " + name + "!") # module2.py def say_goodbye(name): print("Goodbye, " + name + "!") To use these modules in another Python program, you can import them using the ... Read More
In the English alphabet, letters are categorized into vowels and consonants. The vowels are (a, e, i, o, u), and the remaining alphabetic characters are considered as consonants. In this article, we are going to detect the vowels vs the consonants in Python. Python provides a simple way to solve this problem by using the built-in string methods and logical conditions. Using Python in Operator The in operator is used to check whether the provided assignment character exists in the string. It returns true if it exists; otherwise, it returns false. To detect whether a letter ... Read More
The consonants are the alphabetic characters that are not vowels (a, e, i, o, u). In Python, string manipulation is a common task. In this article, we will explore how to delete consonants from a string using different approaches, including loops, list comprehensions and regular expressions. Using Loop In this approach, we declare a variable containing vowels in both upper and lower case, then loop through each character in the string. If the character is a vowel or a non-alphabet character, we add it to the result ? Example Let's look at the following example, where ... Read More
In Python, there are several methods to check if a given character in a string is an alphabetic letter. Here, we'll explore the most effective techniques: using the isalpha() method, the string module, regular expressions, and Unicode-based approaches. Using the isalpha() Method The isalpha() method is a built-in method in Python that returns True if the character is an alphabetic letter and False otherwise. This is the most straightforward and recommended approach ? text = "Hello World" index = 1 if text[index].isalpha(): print(f"The character '{text[index]}' at index {index} is a letter") ... Read More
A bytestring in Python is a sequence of bytes, represented using the bytes data type in Python 3. Bytestrings are primarily used to handle binary data or data that doesn't conform to ASCII or Unicode encodings, such as images, audio files, and network protocols. They are crucial for tasks that require low-level data manipulation. Creating a Bytestring To create a bytestring in Python, prefix a string literal with the letter b. This indicates to Python that the string should be interpreted as a sequence of bytes ? bytestring = b"This is a bytestring." print(bytestring) print(type(bytestring)) ... Read More
String wrapping means formatting or breaking text so that it fits within a specified width. This is useful when writing content to files, such as logs, reports, or console-like outputs that need consistent line lengths. Python provides built-in methods through the textwrap module to wrap strings before writing them to files. Let's explore different approaches to wrap strings in files. Using textwrap.wrap() Method The textwrap.wrap() method splits a single paragraph into a list of lines, each at most a specified width. Syntax textwrap.wrap(text, width) Example Here's how to wrap a string ... Read More
When working with strings in Python, you may need to replace escaped backslashes \ with single backslashes \. This process is called unescaping. Python provides several methods to handle this conversion. Using ast.literal_eval() The ast.literal_eval() method safely evaluates a string containing a literal expression. To use this method, surround the string with an additional layer of quotes ? import ast # String with escaped backslashes escaped_string = '"Hello, world"' result = ast.literal_eval(escaped_string) print(result) Hello, world Using raw strings with replace() A simpler approach is using the replace() method with ... Read More
Sorting strings that contain numbers, such as ("xy1", "xy2", "xy10"), can be complex in Python. For example, if we sort the list ["xy1", "xy2", "xy10"] using the built-in sort() method, it results in ["xy1", "xy10", "xy2"]. But we expect "xy2" to come before "xy10". This happens because Python's default sorting uses lexicographical order and compares characters from left to right based on their Unicode values. Since the character '1' in "xy10" comes before "2" in "xy2", the "xy10" is treated as smaller, even though the number 10 is greater than 2. This is where we need natural sorting, ... Read More
In this article, we are going to learn how to replace the last occurrence of an expression in a string. In Python, String manipulation is a common task, and Python provides the built-in method named replace(). Though we can replace the specified character or substring in a string using this method, it does not directly support replacing the last occurrence. To achieve this we need to use slicing, string splitting, or a regular expression. Using rfind() Method The rfind() method searches for the starting index of the last occurrence of the specified substring. We can use this ... Read More
A string is a collection of characters that can represent a single word or a whole sentence. While working with strings, we often need to check whether the string starts with a capital letter. This can be useful for validating names or sentences to ensure they follow certain formatting rules. Python provides multiple methods to test if a string starts with a capital letter. In this article, we will explore three different approaches to accomplish this task. Using str.isupper() Method The isupper() method checks whether all characters in a string are uppercase. To test if a string ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance