20 String Methods in Python That Are Commonly Used by Data Scientists
A reference list of Python string methods for data scientists
5 min readDec 5, 2022
- str.lower() — returns a copy of the string with all the characters in lowercase.
name = "JOHN DOE"
# Convert to lowercase
print(name.lower()) # Output: "john doe"
2. str.upper() — returns a copy of the string with all the characters in uppercase.
name = "john doe"
# Convert the string to uppercase
print(name.upper()) # Output: "JOHN DOE"
3. str.strip() — returns a copy of the string with leading and trailing whitespace removed.
sentence = " This is a sample sentence. "
# Strip leading and trailing whitespace from the string
stripped_sentence = sentence.strip()
print(stripped_sentence) # Output: "This is a sample sentence."
4. str.isalpha() — returns True
if all the characters in the string are alphabetic, and False
otherwise.
string1 = "Hello"
string2 = "Hello123"
# Check if the string consists only of alphabetic characters
is_alpha1 = string1.isalpha()
is_alpha2 = string2.isalpha()
print(is_alpha1) # Output: True
print(is_alpha2) # Output: False