Python String Methods


Python string operations are ways you can performs action on string.

Python Case Change :-

For given string you can make it UPPERCASE or lowecase or capitalize each letter using lower(), upper() and capitalize() built in methods.

my_string = "Python is the Best"

print(my_string.lower())  # Output: python is the best
print(my_string.upper())  # Output: PYTHON IS THE BEST
print(my_string.capitalize())  # Output: Python Is The Best


Python String Strip :-

strip() Removes leading/start and trailing/end whitespace characters from the string.

text = "   Love Python   "
stripped_text = text.strip()
print(stripped_text)  # Output: Love Python


Python String Replace :-

replace(old, new)  Replaces occurrences of the specified old substring with the new substring.

text = "hello python"
replaced_text = text.replace("python", "replace")
print(replaced_text)  # Output: hello replace


Python String Split :- 

split(separator) Splits the string into a list of substrings based on the specified separator.

text = "This, is, example of, split, with, separator"
split_text = text.split(",")
print(split_text)  # Output: ['This', 'is', 'example of', 'split', 'with', 'separator']


Python String Join :-

join(iterable) Joins the elements of an iterable (e.g., a list) into a single string, using the string as a delimiter.

words = ["hello", "world", "Python"]
joined_text = ",".join(words)

print(joined_text)  # Output: hello,world,Python