Python 3 String Methods
List of string methods available in Python 3.
Method | Description | Examples | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
capitalize() |
Returns a copy of the string with its first character capitalized and the rest lowercased. |
Result
Bee sting |
||||||||||||||||||||||||
casefold() |
Returns a casefolded copy of the string. Casefolded strings may be used for caseless matching. |
Result
bee |
||||||||||||||||||||||||
center(width[, fillchar]) |
Returns the string centered in a string of length width. Padding can be done using the specified fillchar (the default padding uses an ASCII space). The original string is returned if width is less than or equal to len(s) |
Result
----bee----- |
||||||||||||||||||||||||
count(sub[, start[, end]]) |
Returns the number of non-overlapping occurrences of substring (sub) in the range [start, end]. Optional arguments start and end are interpreted as in slice notation. Non-overlapping occurrences means that Python won't double up on characters that have already been counted. For example, using a substring of |
Result
0 5 2 1 0 2 3 |
||||||||||||||||||||||||
encode(encoding="utf-8", errors="strict") |
Returns an encoded version of the string as a bytes object. The default encoding is
|
Result
Banana b'QmFuYW5h' |
||||||||||||||||||||||||
endswith(suffix[, start[, end]]) |
Returns |
Result
True True False True |
||||||||||||||||||||||||
expandtabs(tabsize=8) |
Returns a copy of the string where all tab characters are replaced by one or more spaces, depending on the current column and the given tab size. Tab positions occur every tabsize characters (the default is |
Result
1 2 3 1 2 3 1 2 3 1 2 3 |
||||||||||||||||||||||||
find(sub[, start[, end]]) |
Returns the lowest index in the string where substring sub is found within the slice |
Result
0 -1 3 3 4 -1 -1 |
||||||||||||||||||||||||
format(*args, **kwargs) |
Performs a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces |
Result
Tea and Coffee Coffee and Tea Peas and Beans 1, 2, 3 Lunch: Pizza, Wine |
||||||||||||||||||||||||
format_map(mapping) |
Similar to |
Result
Lunch: Pizza, Wine Lunch: Pizza, Drink Lunch: Food, Wine |
||||||||||||||||||||||||
index(sub[, start[, end]]) |
Like |
Result
0 3 3 4 ValueError: substring not found |
||||||||||||||||||||||||
isalnum() |
Returns A character c is deemed to be alphanumeric if one of the following returns
|
Result
True True False False False |
||||||||||||||||||||||||
isalpha() |
Returns |
Result
True False False |
||||||||||||||||||||||||
isdecimal() |
Returns |
Result
True False False False False False |
||||||||||||||||||||||||
isdigit() |
Returns The |
Result
True True False False False False |
||||||||||||||||||||||||
isidentifier() |
Returns true if the string is a valid identifier according to the language definition, section Identifiers and keywords from the Python docs. |
Result
False True False True True |
||||||||||||||||||||||||
islower() |
Returns |
Result
False False True False False True True |
||||||||||||||||||||||||
isnumeric() |
Returns |
Result
True True False False False False |
||||||||||||||||||||||||
isprintable() |
Returns Nonprintable characters are those characters defined in the Unicode character database as "Other" or "Separator", except for the ASCII space ( |
Result
True True True True False False False |
||||||||||||||||||||||||
isspace() |
Returns Whitespace characters are those characters defined in the Unicode character database as "Other" or "Separator" and those with bidirectional property being one of "WS", "B", or "S". |
Result
False True False True True False |
||||||||||||||||||||||||
istitle() |
Returns Whitespace characters are those characters defined in the Unicode character database as "Other" or "Separator" and those with bidirectional property being one of "WS", "B", or "S". |
Result
False False False True True False True True |
||||||||||||||||||||||||
isupper() |
Returns |
Result
False False True False True False False |
||||||||||||||||||||||||
join(iterable) |
Returns a string which is the concatenation of the strings in iterable. A |
Result
1-2-3 U.S.A Dr. Who |
||||||||||||||||||||||||
ljust(width[, fillchar]) |
Returns the string left justified in a string of length width. Padding can be done using the specified fillchar (the default padding uses an ASCII space). The original string is returned if width is less than or equal to len(s) |
Result
bee--------- |
||||||||||||||||||||||||
lower() |
Returns a copy of the string with all the cased characters converted to lowercase. |
Result
bee |
||||||||||||||||||||||||
lstrip([chars]) |
Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or set to |
Result
Bee ! Bee----- |
||||||||||||||||||||||||
maketrans(x[, y[, z]]) |
This is a static method that returns a translation table usable for
|
Result
123425 6782 |
||||||||||||||||||||||||
partition(sep) |
Splits the string at the first occurrence of sep, and returns a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, it returns a 3-tuple containing the string itself, followed by two empty strings. |
Result
('Python', '-', 'program') ('Python-program', '', '') |
||||||||||||||||||||||||
replace(old, new[, count]) |
Returns a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is provided, only the first count occurrences are replaced. For example, if count is |
Result
Coffee bag. Coffee cup. Coffee leaves. Coffee bag. Coffee cup. Tea leaves. |
||||||||||||||||||||||||
rfind(sub[, start[, end]]) |
Returns the highest index in the string where substring sub is found, such that sub is contained within |
Result
0 8 10 9 -1 -1 -1 |
||||||||||||||||||||||||
rindex(sub[, start[, end]]) |
Like |
Result
0 8 10 9 ValueError: substring not found ValueError: substring not found ValueError: substring not found |
||||||||||||||||||||||||
rjust(width[, fillchar]) |
Returns the string right justified in a string of length width. Padding can be done using the specified fillchar (the default padding uses an ASCII space). The original string is returned if width is less than or equal to len(s) |
Result
---------bee |
||||||||||||||||||||||||
rpartition(sep) |
Splits the string at the last occurrence of sep, and returns a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, it returns a 3-tuple containing the string itself, followed by two empty strings. |
Result
('Homer-Jay', '-', 'Simpson') ('', '', 'Homer-Jay-Simpson') |
||||||||||||||||||||||||
rsplit(sep=None, maxsplit=-1) |
Returns a list of the words in the string, using sep as the delimiter string. If Except for splitting from the right, |
Result
['Homer', 'Jay', 'Simpson'] ['Homer-Jay', 'Simpson'] |
||||||||||||||||||||||||
rstrip([chars]) |
Return a copy of the string with trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or set to |
Result
Bee ! -----Bee |
||||||||||||||||||||||||
split(sep=None, maxsplit=-1) |
Returns a list of the words in the string, using sep as the delimiter string. If If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, The sep argument may consist of multiple characters (for example, If sep is not specified or is set to |
Result
['Homer', 'Jay', 'Simpson'] ['Homer', 'Jay-Simpson'] ['Homer', '', 'Bart', ''] ['Homer', ',Bart'] ['Homer', 'Bart', 'Marge'] |
||||||||||||||||||||||||
splitlines([keepends]) |
Returns a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the resulting list unless keepends is given and its value is This method splits on the following line boundaries.
|
Result
['Tea', '', 'and coffee', 'cups'] ['Tea\n', '\n', 'and coffee\r', 'cups\r\n'] |
||||||||||||||||||||||||
startswith(prefix[, start[, end]]) |
Returns |
Result
True False True False True |
||||||||||||||||||||||||
strip([chars]) |
Returns a copy of the string with leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or set to |
Result
Bee ! Bee |
||||||||||||||||||||||||
swapcase() |
Returns a copy of the string with uppercase characters converted to lowercase and vice versa. |
Result
hOMER sIMPSON |
||||||||||||||||||||||||
title() |
Returns a title-cased version of the string. Title case is where words start with an uppercase character and the remaining characters are lowercase. |
Result
Tea And Coffee Tea And Coffee |
||||||||||||||||||||||||
translate(table) |
Returns a copy of the string in which each character has been mapped through the given translation table. The table must be an object that implements indexing via |
Result
123425 6782 |
||||||||||||||||||||||||
upper() |
Returns a copy of the string with all the cased characters converted to uppercase. |
Result
BEE |
||||||||||||||||||||||||
zfill(width) |
Returns a copy of the string left filled with ASCII |
Result
00036 -0036 +0036 |