0% found this document useful (0 votes)
21 views9 pages

Strings Are A Group of Alphabets or Words. K 'Iglobal' Type (K) Len (K) 7 K (0) 'I'

The document discusses strings in Python including defining and accessing characters in strings, string slicing, string methods like index and count, and using for loops to iterate through characters in strings.

Uploaded by

Krishnaprasad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
21 views9 pages

Strings Are A Group of Alphabets or Words. K 'Iglobal' Type (K) Len (K) 7 K (0) 'I'

The document discusses strings in Python including defining and accessing characters in strings, string slicing, string methods like index and count, and using for loops to iterate through characters in strings.

Uploaded by

Krishnaprasad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 9

Strings:

Strings are a group of alphabets or words.

>>> k='iglobal'

>>> type(k)

<class 'str'>

>>> len(k)

>>> k[0]

'i'

Index

>>> k='iglobal'

>>> k.index('o')

>>> k.index('l')

>>>

String Slicing

>>> k[0]

'i'

>>>

>>> k[1:4]

'glo'

>>> k[4:7]

'bal'
>>> k[1:6]

'globa'

>>> a='iglobal'

>>> a

'iglobal'

>>> a[3]

'o'

>>> len(a)

>>> a[6:5]

''

>>> a[6:-1]

''

>>> a[3:-1]

'y'

>>> a[3:]

'ya'

>>> a[3:4]

'y'

>>> a[2:]

'vya'

>>> a[:4]

'navy'

>>> a[:]
'navya'

>>> a

'navya'

>>> a[:]

'navya'

>>> a='navya'

>>> a[1:3]+a[0:2]

'avna'(coded values(cyphertext))

>>> a[::-1]

'labolgi'

>>> a[:]

'iglobal'

>>> a[::-2]

'lbli'

>>> a[::0]

Traceback (most recent call last):

File "<pyshell#20>", line 1, in <module>

a[::0]

ValueError: slice step cannot be zero

For loops for strings

Syntax:

for i in 'string or string variable'

#sample program on 'for Loop' in Strings

k='iam learning python'


for i in k:

print(i)

#to print the combinations of letter from two strings

a='yamuna'

b='iglobal'

count=0

for i in a:

for k in b:

print(i+k)

count=count+1

print('there are', count,'combinations')


#To print repetition of each unique letter in a string

a=input('enter the value of first string ')

st=''

for j in a:

count=0

if j not in st:

for k in a:

if(j==k):

count=count+1

print('the letter',j,'is repeated',count,'times')

st=st+j
Second Method:

#sample program to print repetitions of each letter in a string

#mississippi

n=input('enter the value of n ')#mississippi

k=list(n)

for i in set(k):

print(i,'repeated for',k.count(i),'times')
#sample program on 'for Loop' for even position in Strings

k='iam learning python'

l=len(k)

print(l)

for i in range(l):

if(i%2==0): #selects only the even number

print(i,'postion character is', k[i]) #display characters in even poistions


#sample program on 'for Loop' in Strings for both even and odd positions

#even positions on one list and odd positions in another list(Task)

k='iam learning python'

l=len(k)

for i in range(l):

if(i%2==0):

print(i, 'the letter in the even position is',k[i])

print('------------------------------')

for i in range(l):

if(i%2!=0):

print(i, 'the letter in the odd position is', k[i])

You might also like