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
Articles by Tarun Chandra
13 articles
How to check whether a string starts with XYZ in Python?
A string is a collection of characters stored as a single value. Unlike other technologies, there is no need to explicitly declare strings in Python — you just need to assign strings to a variable, making Python strings easy to use. Python provides several methods to check if a string starts with a specific substring. The most common approaches are using the built-in startswith() method and regular expressions. Using startswith() Method The startswith() method is the simplest and most efficient way to check if a string begins with a specific substring. It returns True if the string ...
Read MoreHow can I tell if a string repeats itself in Python?
In Python, you can check if a string is composed of repeating substrings using several approaches. A string repeats itself when it's made up of one or more occurrences of a smaller pattern. Using String Slicing and find() The most elegant approach uses string concatenation and the find() method. By searching for the original string within a doubled version (excluding first and last characters), we can detect if it has a repeating pattern ? def find_repeating_pattern(s): i = (s + s).find(s, 1, -1) return None if i == ...
Read MoreHow to get integer values from a string in Python?
In Python, extracting integer values from strings is a common task when processing text data. Python provides several approaches including the filter() method, regular expressions, and string manipulation techniques. Using filter() and isdigit() Methods The filter() method creates a new iterator by filtering elements based on a condition. When combined with isdigit(), it extracts only the digit characters from a string − text = "There are 20 teams competing in the Premier League" print("The given string is:") print(text) # Extract digits and join them digits = ''.join(filter(str.isdigit, text)) if digits: ...
Read MoreHow to scan a string for specific characters in Python?
Scanning a string for specific characters is a common task in Python programming. There are several approaches to check if certain characters exist in a string, each suited for different scenarios. Using the 'in' Operator The simplest method is using the in keyword to check if a single character exists in a string. It returns True if the character is found, False otherwise ? text = "Welcome to Tutorialspoint" char = 'T' print(f"String: {text}") print(f"Character to find: '{char}'") print(f"Character found: {char in text}") String: Welcome to Tutorialspoint Character to find: 'T' Character ...
Read MoreHow to check if a string can be converted to float in Python?
When working with user input or data processing, you often need to check if a string can be converted to a float before performing the conversion. Python provides several approaches to validate float conversion safely. Using float() with Exception Handling The most reliable approach uses the float() function with try-except handling. This method attempts the conversion and catches any ValueError that occurs ? def is_float(s): try: float(s) return True except ValueError: ...
Read MoreHow to split string on whitespace in Python?
Sometimes we need to split a string into individual words by separating them at whitespace characters. Python provides several methods to accomplish this task efficiently. Using the split() Method The simplest approach is using the built-in split() method. When called without arguments, it automatically splits on any whitespace character (spaces, tabs, newlines) and removes empty strings from the result ? text = "Hello Everyone Welcome to Tutorialspoint" print("The given string is:") print(text) print("The strings after the split are:") result = text.split() print(result) The given string is: Hello Everyone Welcome to Tutorialspoint ...
Read MoreHow to create a long multi-line string in Python?
In Python, you can create long multi-line strings using several approaches. Each method has its own advantages depending on your specific use case and formatting requirements. Using Triple Quotes The most common approach is using triple quotes (''' or """). This method preserves the exact formatting including line breaks, indentation, and special characters like TAB or NEWLINES ? multiline_string = """Welcome to Tutorialspoint How are you doing? Hope everything is fine Thank you""" print("The multi-line string is:") print(multiline_string) The multi-line string is: Welcome to Tutorialspoint How are you doing? Hope everything is ...
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 MoreHow to invert case for all letters in a string in Python?
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 MoreHow to split at NEWLINEs in Python?
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