
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
Found 10543 Articles for Python

181 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

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

830 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

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

15K+ Views
A string is a collection of characters that can represent a single word or a whole sentence. Unlike Java, there is no need to declare Python strings explicitly; we can directly assign a string value to a literal. While working with the string, we will encounter situations to check whether the string starts with a capital letter. This can be useful for validating the names or sentences to ensure they follow certain formatting rules. Python provides multiple methods to test this condition. In this article, we will explore how to test if a string with a capital letter. ... Read More

444 Views
A string is a collection of characters stored as a single value. Un like 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 find out how to check whether a string starts with XYZ in Python. Using startswith() method One way to ... Read More

23K+ Views
In Python, we will come across situations where we encounter long lines of code that exceed the recommended line length of 79 characters (suggested by the Python style guide). To improve code readability, Python provides several ways to wrap long lines. In this article, we will explore the various methods to wrap long lines in Python. Using Backslash(\) The Backslash(\) is used as the line continuation character in Python. It indicates the compiler that the statement continues on the next line. If we try to place anything (word, space or comment) after the backslash, it will result in ... Read More

9K+ Views
In Python 3, the input() function always returns a string, even if the user enters a number. To check if the user input is an integer, you can use a try-except block and attempt to convert the input string to an integer using the int() function. Here are some code examples that demonstrate different ways to check if raw input is an integer in Python 3: Using a try-except block Example In this example, we use a try-except block to attempt to convert the user's input to an integer using the int() function. If the user enters an integer, the ... Read More