Python provides many built-in string methods that make it easy to work with text. String methods can be used to change the case of text, search for characters, replace text, remove spaces, split strings, and perform many other operations.
String methods are built-in functions provided by Python for working with strings.
name = "rahul"
print(name.upper())
Here, upper() is a string method.
The upper() method converts all alphabetic characters in a string to uppercase.
text = "hello python"
print(text.upper())
The lower() method converts all alphabetic characters to lowercase.
text = "HELLO PYTHON"
print(text.lower())
The capitalize() method converts the first character of a string to uppercase and the remaining characters to lowercase.
text = "hello python"
print(text.capitalize())
The title() method converts the first character of each word to uppercase.
text = "python programming language"
print(text.title())
The swapcase() method changes uppercase characters to lowercase and lowercase characters to uppercase.
text = "Hello Python"
print(text.swapcase())
The strip() method removes spaces and specified whitespace from the beginning and end of a string.
text = " Hello Python "
print(text.strip())
The lstrip() method removes whitespace from the left side of a string.
text = " Hello Python"
print(text.lstrip())
The rstrip() method removes whitespace from the right side of a string.
text = "Hello Python "
print(text.rstrip())
The replace() method replaces one part of a string with another string.
text = "I like Java"
new_text = text.replace("Java", "Python")
print(new_text)
The find() method returns the index of the first occurrence of the specified text.
text = "Hello Python"
print(text.find("Python"))
If the specified text is not found, find() returns -1.
print(text.find("Java"))
The index() method returns the index of the first occurrence of the specified text.
text = "Hello Python"
print(text.index("Python"))
The count() method returns the number of times a substring occurs in a string.
text = "banana"
print(text.count("a"))
The startswith() method checks whether a string starts with a specified value.
text = "Python Programming"
print(text.startswith("Python"))
print(text.startswith("Java"))
The endswith() method checks whether a string ends with a specified value.
filename = "student.pdf"
print(filename.endswith(".pdf"))
print(filename.endswith(".jpg"))
The split() method divides a string into a list. By default, it splits the string at whitespace.
text = "Python is easy"
words = text.split()
print(words)
The join() method combines elements of an iterable into a single string.
words = ["Python", "is", "easy"]
result = " ".join(words)
print(result)
The isalpha() method returns True if all characters in the string are alphabetic and the string is not empty.
print("Python".isalpha())
print("Python123".isalpha())
The isdigit() method returns True if all characters in the string are digits.
print("12345".isdigit())
print("123abc".isdigit())
The isalnum() method returns True if all characters are alphabetic or numeric and the string is not empty.
print("Python123".isalnum())
print("Python 123".isalnum())
The isspace() method returns True if all characters in the string are whitespace characters and the string is not empty.
print(" ".isspace())
print("Hello".isspace())
The islower() method checks whether the cased characters are lowercase, while isupper() checks whether they are uppercase.
text = "hello"
print(text.islower())
print(text.isupper())
text = "HELLO"
print(text.isupper())
text = " python programming "
print(text.strip())
print(text.upper())
print(text.title())
print(text.replace("python", "Java"))
print(text.count("p"))
| Method | Purpose |
|---|---|
upper() |
Converts text to uppercase |
lower() |
Converts text to lowercase |
capitalize() |
Capitalizes the first character |
title() |
Capitalizes each word |
strip() |
Removes surrounding whitespace |
replace() |
Replaces text |
find() |
Finds the position of text |
count() |
Counts occurrences |
split() |
Splits a string into a list |
join() |
Joins elements into a string |
Question: Which method converts a string into uppercase letters?