🛠 String Methods
Last Updated: 10th August 2025
String Method means a inbuilt tools that are used to perform operations on strings. Some of them are:
- len() – Built-in Function (Not a Method),Returns the number of characters in a string (including spaces).
text = "Python"
print(len(text)) # 6
- strip() – Remove spaces from both ends,i.e start and end of the string.
text = " Python "
print(text.strip()) # Python
- lstrip() – Remove spaces from left
text = " Python "
print(text.lstrip()) # Python (Left Space Removed)
- rstrip() – Remove spaces from right
text = " Python "
print(text.rstrip()) # Python (Right Space Removed)
- upper() – Converts a string to upper case.
text = "python"
print(text.upper()) # PYTHON
- lower() – Converts a string to lower case.
text = "PYTHON"
print(text.lower()) # python
- capitalize() – Converts the first character to upper case.
text = "python is fun"
print(text.capitalize()) # Python is fun
- title() – Converts the first character of each word to upper case.
text = "python is fun"
print(text.title()) # Python Is Fun
- swapcase() – Converts upper case characters to lower case and vice versa.
text = "PYTHON is Fun"
print(text.swapcase()) # python IS fUN
- isalnum() – Returns True if all characters in the string are alphanumeric (a-z, A-Z and 0-9).
text = "Python"
print(text.isalnum()) # True
- isalpha() – Returns True if all characters in the string are alphabetic (a-z and A-Z).
text = "Python"
print(text.isalpha()) # True
- isdigit() – Returns True if all characters in the string are digits (0-9).
text = "123"
print(text.isdigit()) # True
- islower() – Returns True if all characters in the string are lower case.
text = "python"
print(text.islower()) # True
- isupper() – Returns True if all characters in the string are upper case.
text = "PYTHON"
print(text.isupper()) # True
- isspace() – Returns True if all characters in the string are spaces.
text = " "
print(text.isspace()) # True
- startswith() – Returns True if the string starts with the specified characters.
text = "Python"
print(text.startswith("Py")) # True
- endswith() – Returns True if the string ends with the specified characters.
text = "Python"
print(text.endswith("on")) # True
- replace() – Returns a string where a specified value is replaced with a specified value.
text = "Python"
print(text.replace("Python", "Java")) # Java
- find() – Return first index of substring (or -1 if not found)
text = "banana"
print("banana".find("na")) # 2
- rfind() – Return last index of substring (or -1 if not found)
text = "banana"
print("banana".rfind("na")) # 4
- count() – Count occurrences of substring
text = "banana"
print("banana".count("na")) # 2
- zfill() – Add leading zeros to make string a given length
print("42".zfill(5)) # 00042
- center() – Return a centered string
print("Python".center(10, "*")) # **Python**
- partition() – Split into tuple (before, separator, after)
print("Hello World".partition(" ")) # ('Hello', ' ', 'World')
- rpartition() – Partition from the right
print("Hello World Python".rpartition(" "))
# ('Hello World', ' ', 'Python')
- removesuffix() – Remove suffix from string
print("Hello World".removesuffix("World")) # Hello
- removeprefix() – Remove prefix from string
print("Hello World".removeprefix("Hello")) # World
- split() – Convert string to list
text = "apple,banana,cherry"
print(text.split(","))
# ['apple', 'banana', 'cherry']
Hinglish Tip 🗣: String methods ka use kar ke hum text ko modify, check, format, ya manipulate kar sakte hain. len() method nahi, ek built-in function hai jo har type (string, list, tuple) ka size batata hai.
💡 Quick Practice
- Take " Hello World " and remove spaces from both ends.
- Convert "python programming" to title case.
- Replace "Java" with "Python" in "I like Java".
- Check if "12345" is a digit-only string.
- Count how many times "is" appears in "This is Python and it is fun"