Strings

A string is an immutable sequence of characters that's treated as a value. Strings support all of the immutable sequence functions and operators that result in a new string. For example, "abcdef"[1:4] results in the output "bcd".

In Python, characters are represented by strings of length one.

Strings literals are defined by the use of single or triple quoting. Strings that are defined using single quotes can't span lines, while strings that are defined using triple quotes can. You can enclose a string in single quotes (') or double quotes ("). A quoting character may contain the other quoting character un-escaped or the quoting character escaped, that's preceded by the backslash (\) character.

Examples

"This is a string"
'This is also a string'
"It's a string"
'This book is called "Python Scripting and Automation Guide."'
"This is an escape quote (\") in a quoted string"

Multiple strings separated by white space are automatically concatenated by the Python parser. This makes it easier to enter long strings and to mix quote types in a single string. For example:

"This string uses ' and " 'that string uses ".'

This results in the following output:

This string uses ' and that string uses ".

Strings support several useful methods. Some of these methods are given in the following table.

Table 1. String methods
Method Usage
s.capitalize() Initial capitalize s
s.count(ss {,start {,end}}) Count the occurrences of ss in s[start:end]
s.startswith(str {, start {, end}})
s.endswith(str {, start {, end}}) 
Test to see if s starts with str
Test to see if s ends with str
s.expandtabs({size}) Replace tabs with spaces, default size is 8
s.find(str {, start {, end}})
s.rfind(str {, start {, end}})
Finds first index of str in s; if not found, the result is -1. rfind searches right to left.
s.index(str {, start {, end}})
s.rindex(str {, start {, end}})
Finds first index of str in s; if not found: raise ValueError. rindex searches right to left.
s.isalnum Test to see if the string is alphanumeric
s.isalpha Test to see if the string is alphabetic
s.isnum Test to see if the string is numeric
s.isupper Test to see if the string is all uppercase
s.islower Test to see if the string is all lowercase
s.isspace Test to see if the string is all whitespace
s.istitle Test to see if the string is a sequence of initial cap alphanumeric strings
s.lower()
s.upper()
s.swapcase()
s.title()
Convert to all lower case
Convert to all upper case
Convert to all opposite case
Convert to all title case
s.join(seq) Join the strings in seq with s as the separator
s.splitlines({keep}) Split s into lines, if keep is true, keep the new lines
s.split({sep {, max}}) Split s into "words" using sep (default sep is a white space) for up to max times
s.ljust(width)
s.rjust(width)
s.center(width)
s.zfill(width)
Left justify the string in a field width wide
Right justify the string in a field width wide
center justify the string in a field width wide
Fill with 0.
s.lstrip()
s.rstrip()
s.strip()
Remove leading white space
Remove trailing white space
Remove leading and trailing white space
s.translate(str {,delc}) Translate s using table, after removing any characters in delc. str should be a string with length == 256.
s.replace(old, new {, max}) Replaces all or max occurrences of string old with string new