Python String Methods
Method |
Description |
capitalize() |
Converts the first character to upper
case Example: >>>
'true'.capitalize() 'True' >>>
'language python'.capitalize() 'Language python' |
len() |
Returns length of string. Example: >>>
s='Python Language' >>>
len(s) 15 >>>
len("International") 13 |
count() |
Returns the number of times a
specified value occurs in a string Syntax
string.count(value,
start, end) Parameter Values
value-Required. A String. The string to value to search for start-Optional. An Integer. The position to start the search. Default
is 0 end-Optional. An Integer. The position to end the search. Default
is the end of the string Example: >>>
str1="This is Python string class and
this in interesting." >>>
value="is" >>>
print("This string 'is'occurs :",str1.count(value)) This string 'is'occurs : 3 >>>
print("This string 'is'occurs :",str1.count(value,10)) This string 'is'occurs : 1 |
find() |
Searches the string for a specified
value and returns the position of where it was found Syntax
string.count(value,
start, end) Parameter Values
value-Required. A String. The string to value to search for start-Optional. An Integer. The position to start the search. Default
is 0 end-Optional. An Integer. The position to end the search. Default
is the end of the string Example >>>
str1="This is Python string class and this in interesting." >>>
value="is" >>>
print("This string 'is'occurs :",str1.find(value)) This string 'is'occurs : 2 >>>
print("This string 'is'occurs :",str1.find(value,10)) This string 'is'occurs : 34 |
format() |
Formats specified values in a string Example: >>>
txt1 = "My name is {fname}, I'am {age}".format(fname =
"John", age = 36) >>>
txt1 "My name is John, I'am 36" >>>
txt2 = "My name is {0}, I'am {1}".format("John",36) >>>
txt2 "My name is John, I'am 36" >>>
txt3 = "My name is {}, I'am {}".format("John",36) >>>
txt3 "My name is John, I'am 36" |
index() |
Searches the string for a specified
value and returns the position of where it was found Example: >>>txt = ”Hello, welcome to my world.” |
isalnum() |
Returns True if all characters in the
string are alphanumeric Example: >>>
s=“CBSEExam2020” >>>
print(s.isalnum()) True >>>
s=“CBSEExam-2020” >>>
print(s.isalnum()) False |
isalpha() |
Returns True if all characters in the
string are in the alphabet Example: >>>
s=“Python” >>>
print(s.isalpha()) True >>>
s=“Python 3.8.3” >>>
print(s.isalpha()) False |
isdigit() |
Returns True if all characters in the string are
digits Example: >>>
s="CBSE2020" >>>
s.isdigit() False >>>
s="2020" >>>
s.isdigit() True >>>
s="CBSE-2020" >>>
s.isdigit() False |
islower() |
Returns True if all characters in the
string are lower case Example >>>
s="cbse" >>>
s.islower() True >>>
s="CBSEBOARD" >>>
s.islower() False |
isnumeric() |
Returns True if all characters in the
string are numeric Example: >>> s="2020" >>> s.isnumeric() True >>> s="CBSE2020" >>> s.isnumeric() False >>> num=2020 >>> num.isnumeric() Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
num.isnumeric() AttributeError: 'int' object has no
attribute 'isnumeric' |
isupper() |
Returns True if all characters in the
string are upper case Example: >>>
s=“cbse" >>>
s.isupper() False >>>
s="CBSEBOARD” >>>
s.isupper () True |
join() |
Joins the elements of an iterable to
the end of the string Example: 1. Connecting
elements using an empty string >>>
array = ['H','E','L','L','O'] >>>
connector = "" >>>
joined_string = connector.join(array) >>> print(joined_string) HELLO 2. Connecting
using a comma >>>
array = ['1','2','3','4','5'] >>>
connector = "," >>>
joined_string = connector.join(array) >>>
print(joined_string) 1,2,3,4,5 3.
Connecting without declaring a separate
string variable >>>
array = ['W','O','R','L','D'] # >>>
print("-".join(array)) W-O-R-L-D |
lower() |
Converts a string into lower case Example: >>>
s="CBSE2020" >>>
s.lower() 'cbse2020' >>>
s="CBSE board" >>>
s.lower() 'cbse board' |
upper() |
Converts a string into upper case Example: >>>
s="CBSE board" >>>
s.upper() 'CBSE BOARD' >>>
s="cbse board 2020" >>>
s.upper() 'CBSE BOARD 2020' |
partition() |
Returns a tuple where the string is
parted into three parts Example: # Split the string on 'and' >>> S = 'Do it now and keep it simple' >>> x = S.partition('and') >>> print(x) ('Do it now ', 'and', ' keep it
simple') # If the separator is not found >>> S = 'Do it now and keep it simple' >>> x = S.partition('or') >>> print(x) ('Do it now and keep it simple', '',
'') >>> S = 'Do it now and keep it simple' >>> x = S.partition('it') # If the separator is present
multiple times, the method splits the string at the first occurrence >>> print(x) ('Do
', 'it', ' now and keep it simple') |
replace() |
Returns a string where a specified
value is replaced with a specified value Example: |
strip() |
Returns a trimmed version of the
string Example: >>>
S = 'xxxxSxxxxSxxxx' >>>
x = S.strip('x') >>>
print(x) SxxxxS >>>
S = '... - Version 3.2 Model-32 ...' >>>
x = S.strip('.- ') >>> print(x) Version 3.2 Model-32 |
lstrip() |
Returns a left trim version of the
string Example: >>>
S = 'xxxxSxxxxSxxxx' >>>
x = S.lstrip('x') >>>
print(x) SxxxxSxxxx >>>
S = '... - Version 3.8 Model-32' >>>
x = S.lstrip('.- ') >>>
print(x) Version 3.2 Model-32 |
rstrip() |
Returns a right trim version of the
string Example: >>> S = 'xxxxSxxxxSxxxx' >>> x = S.rstrip('x') >>> print(x) xxxxSxxxxS |
split() |
Splits the string at the specified
separator, and returns a list Example: >>>
s="cbse board 2020" >>>
s 'cbse board 2020' >>>
s.split('board') ['cbse ', ' 2020'] #converts into list >>>
s.split(' ') ['cbse', 'board', '2020'] #converts into list |
swapcase() |
Swaps cases, lower case becomes upper
case and vice versa Example: >>>
s="Swap Case of all Characters in a String" >>>
s.swapcase() 'sWAP cASE OF ALL cHARACTERS IN A sTRING' |
title() |
Converts the first character of each
word to upper case Example: >>>
S = 'hello, world!' >>>
x = S.title() >>>
print(x) Hello, World! >>>
S = "c3po is a droid" >>>
x = S.title() >>>
print(x) C3Po Is A Droid >>>
S = "they're bob's friends." >>>
x = S.title() >>>
print(x) They'Re Bob'S Friends |
No comments:
Post a Comment