Last modified: Jan 10, 2023 By Alexander Williams

String operations in python with example program

capitalize()

Converts the first character to upper case


str1 = "python"
print(str1.capitalize())

output

Python

casefold()

Converts string into lower case


str1 = "PYTHON"
print(str1.casefold())

output

python

center()

Returns a centered string


str1 = "PYTHON"
print(str1.center(10))

output

python

count()

Returns the number of times a specified value occurs in a string


str1 = "python"
print(str1.count("p"))

output

1

endswith()

Returns true if the string ends with the specified value


str1 = "python"
print(str1.endswith("n"))

output

True

index()

Searches the string for a specified value and returns the position of where it was found


str1 = "python"
print(str1.index("h"))

output

3

islower()

Returns True if all characters in the string are lower case


str1 = "python"
print(str1.islower()

output

True

for more operations check out this list bellow

Python String Methods - W3Schools