String functions for revision in Python: Let us Revise what we did learn!!
Essential String Functions in Python:
len(string)
: Returns the length of the string (number of characters).- Example:
length = len("Hello, World!") # length will be 13
- Example:
string.upper()
: Converts all lowercase letters in the string to uppercase.- Example:
uppercase_text = "hello".upper() # uppercase_text will be "HELLO"
- Example:
string.lower()
: Converts all uppercase letters in the string to lowercase.- Example:
lowercase_text = "HELLO".lower() # lowercase_text will be "hello"
- Example:
string.split(sep, maxsplit=None)
: Splits the string into a list of substrings based on the specified separator (sep
). The optionalmaxsplit
parameter limits the number of splits to occur.- Example:
words = "apple,banana,cherry".split(",") # words will be ["apple", "banana", "cherry"]
- Example:
string.join(iterable)
: Joins elements from an iterable (e.g., list) into a single string using the specified separator.- Example:
joined_string = "-".join(["apple", "banana", "cherry"]) # joined_string will be "apple-banana-cherry"
- Example:
string.strip(chars=None)
: Removes leading and trailing characters from the string. Optionally, you can specify characters to remove (chars
).- Example:
clean_text = " Extra spaces ".strip() # clean_text will be "Extra spaces"
- Example:
string.replace(old, new, count=None)
: Replaces occurrences of a substring (old
) with another substring (new
). The optionalcount
parameter limits the number of replacements.- Example:
fixed_text = "Mississippi".replace("ss", "s", 1) # fixed_text will be "Misispippi"
- Example:
string.startswith(prefix, start=0, end=None)
: Checks if the string starts with the specified prefix within a given range.- Example:
does_start = "Hello, World!".startswith("Hello") # does_start will be True
- Example:
string.endswith(suffix, start=None, end=endobj)
: Checks if the string ends with the specified suffix within a given range.- Example:
does_end = "Hello, World!".endswith("World!") # does_end will be True
- Example:
string.find(sub, start=0, end=None)
: Returns the index of the first occurrence of the substring (sub
) within a given range, or -1 if not found.
- Example:
first_index = "Hello, World!".find("W") # first_index will be 7
string.rfind(sub, start=0, end=None)
: Returns the index of the last occurrence of the substring (sub
) within a given range, or -1 if not found.
- Example:
last_index = "Hello, World! World!".rfind("World") # last_index will be 17
string.isalpha()
: Checks if all characters in the string are alphabetic (letters a-z and A-Z).
- Example:
is_alpha = "hello123".isalpha() # is_alpha will be False
string.isdigit()
: Checks if all characters in the string are digits (0-9).
- Example:
is_digit = "12345".isdigit() # is_digit will be True
string.isalnum()
: Checks if all characters in the string are alphanumeric (letters and digits).
- Example:
is_alnum = "hello123".isalnum() # is_alnum will be True
string.isspace()
: Checks if all characters in the string are whitespace characters (spaces, tabs, newlines, etc.).
- Example:
is_space = " tn".isspace() # is_space will be True
string.istitle()
: Checks if the string is a titlecased string (first letter of each word uppercase, others lowercase).
- Example: `is_title
Discover more from HintsToday
Subscribe to get the latest posts sent to your email.