Server Side Programming Articles

Page 672 of 2109

How to check if a string contains only decimal characters?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 5K+ Views

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 More

How to get a string left padded with zeros in Python?

Tarun Chandra
Tarun Chandra
Updated on 24-Mar-2026 18K+ Views

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

How to translate a python string according a given dictionary?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 2K+ Views

In Python, translating a string using a dictionary is commonly used for text manipulation and encoding. It is typically achieved by using the str.translate() method along with str.maketrans() to define character mappings. The dictionary passed to maketrans() contains key-value pairs where the keys are characters to replace and values are their corresponding replacements. Let's explore different approaches to translate strings using dictionaries. Using str.translate() with str.maketrans() The str.translate() method uses a translation table generated by str.maketrans() to perform one-to-one character mapping efficiently. Syntax str.translate(table) str.maketrans(x, y, z) Basic Character Replacement Here's ...

Read More

How to invert case for all letters in a string in Python?

Tarun Chandra
Tarun Chandra
Updated on 24-Mar-2026 3K+ Views

A string is a sequence of characters that can represent words, sentences, or any text data. In Python, you can manipulate string cases using various built-in methods. In this article, we will explore how to invert the case for all letters in a string in Python using different approaches. Using the swapcase() Method The most efficient way to invert case is using the built-in swapcase() method. This method converts all lowercase letters to uppercase and all uppercase letters to lowercase ? Syntax string.swapcase() Example Here's how to use swapcase() to invert ...

Read More

How to split at NEWLINEs in Python?

Tarun Chandra
Tarun Chandra
Updated on 24-Mar-2026 32K+ Views

In Python, splitting strings at newlines is a common text processing task. Python provides two main approaches: the splitlines() method and the split() method with '' parameter. Using splitlines() Method The splitlines() method is specifically designed for splitting strings at line breaks. It handles various types of line endings automatically and doesn't require any parameters ? Example 1 Splitting a string with escape sequence newlines ? text = "WelcometoTutorialspoint" print("The given string is:") print(text) print("The resultant string split at newline is:") print(text.splitlines()) The given string is: Welcome to Tutorialspoint ...

Read More

How to search a string in backwards direction in Python?

Tarun Chandra
Tarun Chandra
Updated on 24-Mar-2026 4K+ Views

In this article, we are going to find out how to search a string in backward direction in Python. Python provides two main methods for searching strings from right to left: rindex() and rfind(). Both methods find the rightmost occurrence of a substring but handle missing substrings differently. Using rindex() Method The rindex() method returns the highest index of any substring in the given string. By highest index, we mean that if a substring appears multiple times, rindex() returns the index of the rightmost occurrence. The main disadvantage is that it throws a ValueError if the ...

Read More

How to remove all leading whitespace in string in Python?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 18K+ Views

Leading whitespace refers to any spaces, tabs, or other blank characters that appear at the start of a string. These characters come before the first visible text and can affect how the string is displayed or processed. Python provides several methods to remove leading whitespace from strings. The most common and recommended approach is using the lstrip() function. Using the lstrip() Function The lstrip() function removes all whitespace characters from the beginning (left side) of a string. It does not affect any whitespace at the end or in the middle of the string. Syntax ...

Read More

How to join two strings to convert to a single string in Python?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 2K+ Views

In Python, we can join two strings into one single string using different approaches. Each method has its own advantages depending on the use case. Using + Operator The most common way to combine two strings is by using the + operator. It concatenates strings exactly as they are, without any automatic separator ? str1 = "Hello" str2 = "World" # Direct concatenation without space result1 = str1 + str2 print("Without space:", result1) # Adding space manually result2 = str1 + " " + str2 print("With space:", result2) Without space: HelloWorld ...

Read More

How to check if a character is upper-case in Python?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 44K+ Views

In this article, we are going to find out how to check if a character is an uppercase letter in Python. We will explore four different approaches ? Using the isupper() method Using regular expressions Using ASCII values Direct comparison In each approach, we will check whether a given character is in uppercase and return True or False accordingly. Using the isupper() Method The first and most straightforward way is by using the built-in isupper() method. This method ...

Read More

How to check if text is “empty” (spaces, tabs, newlines) in Python?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 5K+ Views

In this article, we will explore different methods to check if text contains only whitespace characters (spaces, tabs, newlines) or is completely empty in Python. Using isspace() Method The isspace() method determines whether a string contains only whitespace characters. It returns True if the string consists entirely of spaces, tabs, newlines, or other whitespace characters; otherwise, it returns False. Note that isspace() returns False for empty strings ⁠— it only detects strings with whitespace content. Example Let's test different strings to see how isspace() behaves ⁠— text1 = " " ...

Read More
Showing 6711–6720 of 21,090 articles
« Prev 1 670 671 672 673 674 2109 Next »
Advertisements