
There are a set of Python String Methods which can be used with strings and very helpful for developers while working with Strings in Python programming language. You can use Python string methods to return new values without making any change in Strings.
After you gather knowledge in Python Strings then you can start learning Python String methods to boost your productivity while interacting with this element in Python. In this article we will learn about some of the built-in methods available in Python Strings.
| # 1. Creating a string greeting = "Hello, world!" name = 'Alice' # 2. Printing a string print(greeting) print("My name is", name) |
![]() |
| Method | Description |
| lower() | Converts string to lowercase |
| upper() | Converts string to uppercase |
| capitalize() | Capitalizes the first letter |
| title() | Capitalizes the first letter of every word |
| strip() | Removes whitespace from both ends |
| lstrip() | Removes whitespace from the left side |
| rstrip() | Removes whitespace from the right side |
| replace(old, new) | Replaces parts of a string |
| split(separator) | Splits string into a list using a separator |
| join(iterable) | Joins elements of a list with a string separator |
| find(sub) | Returns index of first occurrence, or -1 if not found |
| index(sub) | Like find(), but raises error if not found |
| count(sub) | Counts how many times a substring occurs |
| startswith(sub) | Checks if string starts with a substring |
| endswith(sub) | Checks if string ends with a substring |
| isalnum() | Returns True if all characters are alphanumeric |
| isalpha() | Returns True if all characters are alphabetic |
| isdigit() | Returns True if all characters are digits |
| isspace() | Returns True if all characters are whitespace |
| islower() | Returns True if all cased characters are lowercase |
| isupper() | Returns True if all cased characters are uppercase |
| var = “PW Skills is an Online Learning Platform.” var_new = var.lower() print (var_new) |
![]() |
| var = “PW Skills is an Online Learning Platform.” var_new = var.upper() print (var_new) |
![]() |
| string. Replace (old, new) |
| text = "I love PWSkills" new_text = text.replace("PWSkills", "PhysicsWallah") print(new_text) word = "banana" print(word.replace("a", "o")) sentence = "The sky is blue. Blue is beautiful." print(sentence.replace("Blue", "Red")) |
![]() |
| text = "Python is powerful and fun" print(text.split()) csv_data = "name,age,city" print(csv_data.split(",")) |
![]() |
| text = "one two three four" print(text.rsplit(" ", 1)) print(text.rsplit(" ", 2)) |
![]() |
| string.find (substring) |
| text = "PW Skills" print(text.find("Skills")) |
| string.find(substring, start, end) |
| print(text.find("k", 0, 5)) |
![]() |
| string.rfind(substring, start, end) |
| text = "Look for the last 'o' in this sentence." index = text.rfind("o") print(index) |
![]() |
Read More: Python If Else Statement: Complete Explanation For Beginners
| isalpha() | Checks for alphabetic value in the string. It will return true if all values are alphabets in the string. |
| isalnum() | Checks for alphanumeric values in the string. It will return true if all values are alphabets as well as numerals in the string. |
| isascii() | Checks for ascii value in the string. It will return true if all characters are ascii in the string. |
| isdigit() | Checks for digit value in the string. It will return true if all characters are digits or numbers in the string. |
| isidentifier() | Checks for identifier value in the string. It will return true if all characters are identifiers in the string. |
| text1 = "Python3" text2 = "Python 3" text3 = "12345" text4 = "Hello!" print(text1.isalnum()) print(text2.isalnum()) print(text3.isalnum()) print(text4.isalnum()) |
![]() |
| string.count(substring, start, end) |
| text = "banana" print(text.count("a")) print(text.count("na")) print(text.count("a", 2, 5)) |
![]() |