0% found this document useful (0 votes)
4 views11 pages

Python String Methods

Uploaded by

rizafatin07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

Python String Methods

Uploaded by

rizafatin07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

PYTHON STRING

METHODS

len()
len(<str>)
Returns the length of the string <str>.

.capitalize()
<str>.capitalize()
Capitalizes the first character of the string <str>, if
it is a letter. Otherwise returns back the original
string <str>.

.split()
<str>.split([sep], [maxsplit])
Breaks up the string <str> at the separator [sep]
and returns a list of the substrings.
By default: The separator is whitespace (i.e.:
[sep]=‘ ’).
By default: [maxsplit]=-1, meaning it splits up as
much as it can.
.partition()
<str>.partition(<sep>)
Partitions the string <str> at the first occurrence of
the separator <sep> if found, and returns a 3-
element tuple of strings containing the part before
the separator, the separator itself, and the part
after the separator.
The separator <sep> cannot be an empty string
(i.e.: <sep>!=‘’).
If the separator <sep> is not found in the given
string <str>, .partition() will return:
(‘<str>’, ‘’, ‘’)
If there is nothing before/after the separator
<sep>, then that part will be an empty string (i.e.:
‘’).

.replace()
<str>.replace(<old>, <new>, [count])
Returns a copy with [count] occurrences of
substring <old> being replaced by <new> in the
given string <str>.
By default: [count]=-1, meaning it replaces all
occurrences of the substring <old> in the given
string with <new>.
[ NOTE: The substring <old> is case-sensitive ]

.find()
<str>.find(<sub>, [start], [end])
Searches the first occurrence and returns the
lowest index of the substring <sub> in the given
string <str> if found.
If the substring <sub> is not found in the given
string <str>, .find() will return:
-1
Negative indexes for [start] and/or [stop] can also
be used, as long as the searching goes from left to
right.
[ NOTE: Negative Index = Positive Index -
len(<str>)
or Positive Index = Negative Index + len(<str>) ]

.index()
<str>.index(<sub>, [start], [end])
Searches the first occurrence and returns the
lowest index of the substring <sub> in the given
string <str> if found.
If the substring <sub> is not found in the given
string <str>, .index() will return:
ValueError: substring not found
Negative indexes for [start] and/or [stop] can also
be used, as long as the searching goes from left to
right.
[ NOTE: Negative Index = Positive Index -
len(<str>)
or Positive Index = Negative Index + len(<str>) ]

.isalpha()
<str>.isalpha()
Returns True if the string <str> is an alphabetic
string (i.e.: contains only letters), False otherwise.
If the string <str> is an empty string (i.e.:
<str>=‘’), .isalpha() will return:
False
[ NOTE: Whitespaces do not count as alphabetic ]

.isdigit()
<str>.isdigit()
Returns True if the string <str> is a digit string
(i.e.: contains only numbers), False otherwise.
If the string <str> is an empty string (i.e.:
<str>=‘’), isdigit() will return:
False
[ NOTE: Whitespaces do not count as digits ]
.isspace()
<str>.isspace()
Returns True if the string <str> is a whitespace
string (i.e.: contains only whitespaces), False
otherwise.
If the string <str> is an empty string (i.e.:
<str>=‘’), .isspace() will return:
False

.isalnum()
<str>.isalnum()
Returns True if the string <str> is an alpha-numeric
string (i.e.: contains only letters or numbers), False
otherwise.
If the string <str> is an empty string (i.e.:
<str>=‘’), .isalnum() will return:
False
[ NOTE: Whitespaces do not count as alphanumeric
]

.islower()
<str>.islower()
Returns True if the string <str> is a lowercase
string (i.e.: all letters are in lowercase), False
otherwise.
If the string <str> does not contain any alphabetic
characters, .islower() will return:
False

.isupper()
<str>.isupper()
Returns True if the string <str> is an uppercase
string (i.e.: all letters are in uppercase), False
otherwise.
If the string <str> does not contain any alphabetic
characters, .isupper() will return:
False

.istitle()
<str>.istitle()
Returns True if the string <str> is a title-cased
string (i.e.: the first letter of each word is in
uppercase and the rest of the letters of that word
are in lowercase), False otherwise.
If the string <str> does not contain any alphabetic
characters, .istitle() will return:
False

.lower()
<str>.lower()
Returns a copy of the string <str> with all
alphabetic characters converted to lowercase.
.upper()
<str>.upper()
Returns a copy of the string <str> with all
alphabetic characters converted to uppercase.

.swapcase()
<str>.swapcase()
Returns a copy of the string <str> with all
uppercase alphabetic characters converted to
lowercase and vice versa.

.title()
<str>.lower()
Returns a copy of the string <str> with all words
converted to title-case.

.count()
<str>.count(<sub>, [start], [end])
Returns the number of occurrences of the substring
<sub> in the given string <str>.
Negative indexes for [start] and/or [stop] can also
be used, as long as the searching goes from left to
right.
[ NOTE: Negative Index = Positive Index -
len(<str>)
or Positive Index = Negative Index + len(<str>) ]
.lstrip()
<str>.lstrip([chars])
If [chars] not provided or if [chars]=None, makes a
copy of string <str>, removes whitespaces on the
left, and returns that copy.
If [chars] provided and is not None, makes a copy
of the string <str>, removes left character if it is in
[chars], goes on until left character is not in
[chars], and returns that copy.

.rstrip()
<str>.rstrip([chars])
If [chars] not provided or if [chars]=None, makes a
copy of string <str>, removes whitespaces on the
right, and returns that copy.
If [chars] provided and is not None, makes a copy
of the string <str>, removes right character if it is
in [chars], goes on until right character is not in
[chars], and returns that copy.

.strip()
<str>.strip([chars])
Returns the result of both .lstrip() and .rstrip()
performed on the string <str>.
.join()
<sep>.join(<iterable>)
Returns a string in which the string elements of the
iterable object <iterable> have been joined by the
string separator <sep>.
<sep> must be a string.
The iterable object <iterable> can be any iterable
(str, list, tuple, dict) as long as all of its elements
are strings.
In the case of dict, only the keys are required to be
strings.

.startswith()
<str>.startswith(<prefix>, [start], [end])
Returns True if the string <str> starts with the
specified prefix <prefix>, False otherwise.
The prefix <prefix> can be a string. It can also be a
tuple of strings with which .startswith() iterates
through to try to see if the string <str> starts with
any of the string elements of the tuple.
If the string <str> is an empty string (i.e.:
<str>=‘’), .startswith() will return:
True

.endswith()
<str>.endswith(<suffix>, [start], [end])
Returns True if the string <str> ends with the
specified suffix <suffix>, False otherwise.
The suffix <suffix> can be a string. It can also be a
tuple of strings with which .endswith() iterates
through to try to see if the string <str> ends with
any of the string elements of the tuple.
If the string <str> is an empty string (i.e.:
<str>=‘’), .endswith() will return:
True

ord()
ord(<char>)
Returns the Unicode ordinal value of the single-
character string <char>.
<char> must be a single-character string.

chr()
chr(<int>)
Returns the single-character string corresponding
to the Unicode ordinal value <int>.
<int> must be in the range of valid Unicode ordinal
values (i.e.: <int> >= 0).

del
del <str>
This removes the address of variable for <str> and
makes it undefined to any value.

[ NOTE: Strings do not support individual character


deletion or substring deletion in the form of
statements, functions, or methods ]

You might also like