3.17 STRING FUNCTIONS AND METHODS
Strings provide methods to perform a variety of useful operations. A method
similar to a function. It takes arguments and returns a value. Some of the function
are listed in the table.
Table 3.11 String Functions in Python
S first character of s
s first letter of each word in s
Count number of occurrences of sub in s
or —1 not found _
s.index(sub) Find first index of sub in s, or raise ValueError if not |
found
“safind(sub) | Same as find, but7 cindex(sud)
Same as inde
dex, but ast index |
Fg tower) Convert s to lower ease
eeword="python programming!
>>>newword=word.upper()
>>>newword:
‘PYTHON PROGRAMMING
this form of dot notation specities the name of the method, “upper” and the name of
ihe string, to apply the method to ‘word’, The empty parentheses indicate that this
inethod takes no arguments,
Amethod call is called an invocation. $
. i is invoking ‘upper’ on ‘word
3.17.2 Lower
The method ‘lower’ takes a string and returns a new string with all lowercase
laters
Exampl
>>>word="PYTHON PROGRAMMING
>>>newword=word.lower()
>>>newword
‘python programming’3.17.3 Capitalize
racter Of 8
e first chat
anction capitalizes the frst
‘python’
>>>s.capitalize()
‘Python’
4 Split
The “split” Function stip leading or trailing white space from # string
Example:
>>>s='python programming!
>>>s.split()
Cpython’, 'programming'}
3.17.5 Find
The ‘find’ method can find substrings in a string
>>>word~python programming!
>>>newword=word.find pro’)
>>>newword
7
Here the ‘find’ method searches from the first and finds the string which sta
pro and returns index 7 as parameter.
>>>newword=word.find(‘pro',3)
>>>newword
7
Here the find method searches from the 3rd index and finds the string which ss
at pro and returns index 7 as parameter.
>>>newword=word.find‘ram,7, 15)
>>enewword
Wne find method searches from then, 0 TO
wit the 7H
em and returns inde, h index to 15th index and finds the si
feat eX 1 a parameten lex and finds the string
>opnewword=worfindrem? 7,15)
>>>newword :
-l
search fails. fina
ere the seat So the ‘fing’ function returns —1 as parameter.
476 The in operator
rie word in is a boolean operator that takes two strings and retums
fist sting APPEATS &S a Substring in the second True’ if the
example:
>>>'ra’ in ‘parrot!
False
>>>'ro! in ‘parrot!
True
python sometimes reads like English, That is, “for (each) letter in (the first) word, if
(ihe) letter (appears) in (the second) word, print (the) letter.”
Example:
>>>def in_both(word1, word2):
for letter in word:
if letter in word2:
print(letter)
>>>in_both(‘jovita’, ‘iesvita’)OT M
3.17.7 String Concatenation 4s 0 i810 40 ting
allows .
Cor python al 1+ \
cans to join, Pythom St inbol
‘Soncatenation operator plus which is denoted by SY
Example:
>>>first
3.17.8 Repetition
i yetition operat
Python allows us to repeat the given string using repel erator whi,
denoted by symbol *.
Example:
>>>a="'Hello!
>>>ats
‘HelloHelloHelloHelloHello
3.17.9 String Length
‘The len function returns the number of characters in a string,
Example:
>>>s='problem solving!
>>>len(s)
15
3.17.10 String comparison
The relational operators work on strings. The relational operators are used
compare two strings.
Example:
>>>if 'python'= ='python’:
print(’Both strings are equal’)
Both strings are equalODL ————— rer
ARINC
we
module provides addition,
pee the standard dat Al tools t
rm gpte 1”
structure
Hipulate strings Som
“is
te:
7
yw
import st
<-ostring digits
23450789"
ingaseli_letters
ny
‘ol
sabedetghijklmnopqrstuywayza
AOPORS aes WwayABCDEFGHUKLM
s>>string.aseii_lowercase
abedefghijklmnopqrstuvwxyz!
ascii_upperease
>FGHUKLMNOPQRSTUVW xyz!
~>string. hexdigit
19123456789abedefABCDEF
s>>string.octdigits
101234567"
>>>string punctuation
HSMN I<
>>>string printable
‘0123456789abcdetighijkimnopqrstuvwxy2ABCDE
FGHUKLMNOPQRSTUVWXYZ!"#S%8')"*=./
<=>2@[\\I_ {I}~ WinlriwOblw0e"
>>>string,whitespace
‘nin\xOb\x0e"
>>>string.capwords("python")
‘Python’
2@h~
M8 LISTS AS ARRAYS
ng array in other programm
list is similar to cre:
tition operator and a
Python solution is to use the
* Creating fixed
languages. The typic
list containing the value None:
eeExample:
[None]*5
a
[None, None, None, None, None]
The variable is now a list with five elements, but the positions do not yet have ,,
usefull value stored in them
1. and 7670 18 a tno
* In some ions the array will hold numeric da
appropria
pli
al value.
Example:
>>>data = [0]*5
A two dimensional array is usually represented in Python by a list of lists. Howey,
the initialization of such a value is not as casy as the initialization of & one
dimensional list.
Example:
>>>a="none’
>>>data=[a]*5
>>>data
[‘none’, ‘none’, ‘none’, ‘none’, ‘none’]
>>>for i in range(0,5):
datafi)=[0}*5
>>>data
[[0, 0, 0, 0, 0}, £0, 0, 0, 0, 0, {0, 0, 0, 0, 0}, £0, 0, 0, 0, 0},{0, 0, 0, 0, 0})
>>>data[2][2]-7
>>>data
{[0, 0, 0, 0, 0}, (0, 0, 0, 0, 0}, [0, 0, 7,0, 0}, {0, 0, 0, 0, 0},{0, 0, 0, 0, OT)