6.1 StringMethods-01.pdf
6.1 StringMethods-01.pdf
The Python STRING type contains various methods that can be used to interface with string values. Below is a list of
some of these methods.
Method Description
len() Returns the length of the string. (Not exclusive to string
types.
IN: len(“Python”)
OUT: 6
str.capitalize() Returns a copy of the string with its first character
capitalized and the rest lowercased.
IN: “school”.count(“o”)
OUT: 2
str.find(sub,[start],[end]) Returns the lowest index in the string where substring sub
is found within the string.
IN: “school”.find(“o”)
OUT: 3
str.isalpha() Returns true if all characters in the string are alphabetic
and there is at least one character.
IN: “Python”.isalpha()
OUT: True
IN: “Python3”.isalpha()
OUT: False
str.isdigit() Returns true if all characters in the string are digits and
there is at least one character.
IN: “123”.isdigit()
OUT: True
IN: “123a”.isdigit()
OUT: False
str.upper() Returns a copy of the string with all the cased characters
converted to uppercase.
IN: “python”.upper()
OUT: “PYTHON”
str.lower() Returns a copy of the string with all the cased characters
converted to lowercase.
IN: “PYTHON”.lower()
OUT: “python”
str.split([sep], [maxsplit]) Returns a list of the words in the string, using sep as the
delimiter string.
IN: “String,Methods”.split(“,”)
OUT: [“String”, “Methods”]
str.strip([chars]) Returns a copy of the string with the leading and trailing
characters removed.