Strings
A string is an immutable sequence of characters that
is 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 cannot span lines, while
strings that are defined using triple quotes can. A string can be
enclosed in single quotes ('
) or double quotes ("
).
A quoting character may contain the other quoting character un-escaped
or the quoting character escaped, that is 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.
Method | Usage |
---|---|
s.capitalize() |
Initial capitalize s |
s.count(ss {,start {,end}}) |
Count the occurrences of ss in s[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 |
|
Finds first index of str in s ;
if not found, the result is -1. rfind searches right
to left. |
|
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 |
|
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 |
|
Left justify the string in a field
width wideRight justify the string in a field width widecenter justify the string in a field width wide Fill with 0. |
|
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 |