Sie sind auf Seite 1von 4

String :

A String is group of character ,A string can hold any type of known characters i.e.
letters,numbers,symbols.

city=”Kathmandu”

K A T H M A N D U
0 1 2 3 4 5 6 7 8
- - - - -5 - - - -
9 8 7 6 4 3 2 1
Forward indexing 0,1,2,3,…..

Backward Indexing -9,-8,-7……

Print city[0] OR print city[-9]  K

Print city[9] OR print city[-1]  Error

City[2]=”A”  Error individual character assignment in string not allowed

Enclosing the string in single,double, triple(for multiline) quote


>>>life”””kv
Kathmandu
EOI “””
>>> print life
kv
Kathmandu
EOI

Triple quotes are used when the text is multiline.

Details

1. Traversing in string:

Name=”KV Kathmandu”

For ch in name

Print (ch,’-‘,end=,” ”)

K-V- -K-a-t-h-m-a-n-d-u

2. Traversing in Reverse order


Name=”KV Kathmandu”

N=len(Name)

For i in range(-1,(-N-1),-1)

Print (ch,’-‘,end=,” ”)

u-d-n-a-m-h-t-………………..

3. String Operators
a. Concat +

b. Replication *

c. Membership in/not in

d. Comparision == / !=

4. String Slicing
City=”Kathmandu”

Print city[2:6] # thma

Print city[2:] #thmandu

Print city[:6] #kathma

Print city[2,-2] #thmand

Built-in Function for String

len(city)- 9
5. Built-in :String Manipulation Methods:
a. String.capitalize()
msg=”kv is our school”
print msg.capitalize():Kv is our school
b. String.find(sub,start,end):to find position of a substring in a given string

Str=”Kathmandu”

Str.find(“th”,1,8) 2

Str.find(“th”,7,8) -1

Str.find(“td”,1,8) -1

c. String.isalnum():it returns true if string is having alphanumeric value otherwise it


returns false
S1=”*”
if (S1.isalnum()) - true
s2=” ”
if (s2.isalnum()) - false

d. String.isalpha()
S1=”*adfddfgfd”
if (S1.isalpha()) -False

e. String.isdigit()

S1=”5”

if (S1.isdigit()) -True

S1=”A”

if (S1.isdigit()) -False

f. String.islower()
S1=”a”
if (S1.islower()) -True

g. String.isupper()

h. String.isspace()
s2=” ”
if (s2.isspace()) -True

i. String.upper()
S1=”kAThManDU”
S2=S1.upper()
print (s2) KATHMANDU
String.lower()

Das könnte Ihnen auch gefallen