Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 3346 of 3366
3K+ Views
In Python, both modules and packages are used to organize and structure code, but they serve slightly different purposes. A Python module is a single file containing Python code that can be imported and used in other Python code. A module can define variables, functions, classes, and other Python constructs that can be used by other code. Modules are a great way to organize code and make it reusable across multiple programs. A Python package, on the other hand, is a collection of related Python modules that are organized in a directory hierarchy. A package can contain one or more ... Read More
292 Views
The zfill() method in Python is used to pad a string with leading zeros to achieve a specified total length. If the string is already longer than the specified length, zfill() does nothing. Syntax Following is the syntax for the zfill() method string.zfill(width) Consistent File Naming Consistent file naming with leading zeros is crucial for sequential processing scripts because it ensures correct sorting and handling of files. Example Here, even though i ranges from 1 to 10, each number is formatted to have three digits with leading zeros, ensuring consistent filename length. for i in range(1, 11): ... Read More
6K+ Views
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 is a ... Read More
1K+ Views
The consonants are the alphabetic characters that is not a vowel (a, e, i, o, u). In Python, String manipulation is the common task, In this article we will explore how to delete consonants from a string using different approaches, including loops, list comprehensions and regular expression. Using Loop In this approach, we are going to declare the variable assignment with vowels in both upper and lower case and then looping through each character in the string. If the character is vowel or a non-alphabet character we will retrieve them and add to result. Example Let's ... Read More
12K+ Views
In Python, there are several methods to check if a given character in a string is an alphabetic letter. Here, we'll explore three effective techniques: using the isalpha() method, the string module, and regular expressions. Using the isalpha() method The isalpha() method is a built-in method in Python that returns True if all the characters in a string are alphabets (letters) and False otherwise. Example In this example, we have a string "Hello World" and we want to check if the character at index 1 is a letter. We use the isalpha() method to check if the character is ... Read More
2K+ Views
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 the ASCII or Unicode encodings, such as images, audio files, and more. 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. Example In this example, we create a bytestring with the contents "This is a ... Read More
3K+ Views
Wrapping the string means formatting or breaking the string so that it fits within the specified width. It is useful when writing the content to the file, such as logs or console-like outputs. Python provides various ways to achieve string wrapping before writing it to the file using the built-in methods. In this article, we are going to learn how to wrap a string in a file in Python. Using Python textwrap.wrap() Method The textwrap.wrap() method is used to wrap a single paragraph of text. so that every line is at most a specified length. It accepts a text ... Read More
898 Views
There are two ways to go about replacing \ with \ or unescaping backslash escaped strings in Python. First is using literal_eval to evaluate the string. Note that in this method you need to surround the string in another layer of quotes. For example:>>> import ast >>> a = '"Hello,world"' >>> print ast.literal_eval(a) Hello, worldAnother way is to use the decode('string_escape') method from the string class. For example,>>> print "Hello,world".decode('string_escape') Hello, world
3K+ Views
Sorting the string that contains the number, 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 will expect "xy2" to come before "xy10". It is because the Python default sorting uses the lexicographical order and compares the 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 ... Read More
8K+ Views
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 or a regular expression. Using Python rfind() Method The first approach is by using the Python rfind() method searches for the starting index of the last occurrence of the specified substring. Here, we ... Read More