python logo

String methods

Python hosting: Host, run, and code Python in the cloud!

Methods can be applied to Python strings, where a string is somet text between quotes. An example of using the string method replace, which replaces the a sub-string in a string:


s = "Hello World"
s = s.replace("World","Python")
print(s)

Related Course:
Python Programming Bootcamp: Go from zero to hero

Overview
Python includes these useful methods for string manipulation:



Method with description
capitalize(str)
Returns first letter capitalized string.
count(str)
Count the frequency of the word str. It can also be applied to words: s.count("Hello")
find(str)
Find the position of the first occurence.
index(str)
Find the position of the first occurence, raise exception if not found.
isdecimal()
Returns true if string is decimal.
isdigit()
Returns true if string contains digits only.
isnumeric()
Returns true if string contains numeric only.
lower()
Returns string in lowercase.
len(str)
Returns the length of str.
upper()
Returns string in uppercase.
replace(old, new)
Replaces the first argument with the second argument.
BackNext





Leave a Reply: