Strings PDF
Strings PDF
Strings PDF
String Coding
Interview Questions
In
Simple Way
1 https://www.youtube.com/durgasoftware
Q1) Write a Program To REVERSE content of the given String by using
slice operator?
1) input: durga
2) output: agrud
3)
4) s = input('Enter Some String to Reverse:')
5) output = s[::-1]
6) print(output)
2 https://www.youtube.com/durgasoftware
Q4) Write a Program To REVERSE order of words present in the given
string?
1) input: Learning Python Is Very Easy
2) output: Easy Very Is Python Learning
3)
4) s=input('Enter Some String:')
5) l=s.split()
6) l1=l[::-1]
7) output=' '.join(l1)
8) print(output)
3 https://www.youtube.com/durgasoftware
12) l1.append(l[i][::-1])
13) i=i+1
14) output=' '.join(l1)
15) print(output)
Q7) Write a program to print the characters present at even index and
odd index seperately for the given string?
1st Way:
Output:
D:\durgaclasses>py test.py
Enter Input String:durgasoftware
Characters present at Even Index:
d
r
a
o
t
a
e
Characters present at Odd Index:
u
g
s
f
w
r
4 https://www.youtube.com/durgasoftware
2nd Way:
Output: RTAEVJIA
1) s1='RAVI'
2) s2='TEJA'
3) output=''
4) i,j=0,0
5) while i<len(s1) or j<len(s2):
6) output=output+s1[i]+s2[j]
7) i=i+1
8) j=j+1
9) print(output)
Output: RTAEVJIA
1) s1='RAVI'
2) s2='TEJA'
3) l=list(map(lambda x,y:x+y,s1,s2))
4) print(''.join(l))
Note: The above program can work if the lengths of 2 strings are same.
5 https://www.youtube.com/durgasoftware
If strings having different lengths:
Output:
D:\durgaclasses>py test.py
Enter First String:RAVIKIRAN
Enter Second String:TEJA
RTAEVJIAKIRAN
D:\durgaclasses>py test.py
Enter First String:RAVI
Enter Second String:TEJAKIRAN
RTAEVJIAKIRAN
Q9) Assume input string contains only alphabet symbols and digits.
Write a program to sort characters of the string, first alphabet
symbols followed by digits?
1) input: B4A1D3
2) output: ABD134
3)
4) s='B4A1D3'
5) alphabets=[]
6) digits=[]
7) for ch in s:
8) if ch.isalpha():
9) alphabets.append(ch)
10) else:
6 https://www.youtube.com/durgasoftware
11) digits.append(ch)
12) output=''.join(sorted(alphabets)+sorted(digits))
13) print(output)
Alternative way:
1) s='B4A1D3'
2) alphabets=''
3) digits=''
4) for ch in s:
5) if ch.isalpha():
6) alphabets+=ch
7) else:
8) digits+=ch
9) output=''
10) for ch in sorted(alphabets):
11) output=output+ch
12) for ch in sorted(digits):
13) output=output+ch
14) print(output)
7 https://www.youtube.com/durgasoftware
Q11) Write a program for the following requirement?
1) input: a3z2b4
2) output: aaabbbbzz (sorted String)
3)
4) s=input('Enter Some String where alphabet symbol should be followed by digit:')
5) target=''
6) for ch in s:
7) if ch.isalpha():
8) x=ch
9) else:
10) d=int(ch)
11) target=target+x*d
12) output = ''.join(sorted(target))
13) print(output)
8 https://www.youtube.com/durgasoftware
Q13) Write a program for the following requirement?
Input: a4k3b2
Output: aeknbd
1) s='a4k3b2'
2) output=''
3) for ch in s:
4) if ch.isalpha():
5) x=ch
6) output=output+ch
7) else:
8) d=int(ch)
9) newc= chr(ord(x)+d)
10) output=output+newc
11) print(output)
1st way:
1) s='AZZZBCDABBCDABBBBCCCCDDDDEEEEEF'
2) output=''
3) for ch in s:
4) if ch not in output:
5) output=output+ch
6) print(output) # AZBCDEF
9 https://www.youtube.com/durgasoftware
2nd way:
1) s='AZZZBCDABBCDABBBBCCCCDDDDEEEEEF'
2) l=[]
3) for ch in s:
4) if ch not in l:
5) l.append(ch)
6) output=''.join(l)
7) print(output) # AZBCDEF
1) s='ABCDABXXXBCDABBBBCCCZZZZCDDDDEEEEEF'
2) s1=set(s)
3) output=''.join(s1)
4) print(output) #CAEZBFD
1) s='ABCDABXXXBCDABBBBCCCZZZZCDDDDEEEEEF'
2) l=[]
3) for ch in s:
4) if ch not in l:
5) l.append(ch)
6)
7) for ch in sorted(l):
8) print('{} occurrs {} times'.format(ch,s.count(ch)))
1) s='ABCDABXXXBCDABBBBCCCZZZZCDDDDEEEEEF'
2) d={}
3) for ch in s:
4) d[ch]=d.get(ch,0)+1
5) for k,v in d.items():
6) print('{} occurrs {} times'.format(k,v))
10 https://www.youtube.com/durgasoftware
For sorting purpose:
1) s='ABAABBCA'
2) output=''
3) d={}
4) for ch in s:
5) d[ch]=d.get(ch,0)+1
6) for k,v in sorted(d.items()):
7) output=output+str(v)+k
8) print(output)
1) s='ABAABBCA'
2) output=''
3) d={}
4) for ch in s:
5) d[ch]=d.get(ch,0)+1
6) for k,v in sorted(d.items()):
7) output=output+k+str(v)
8) print(output)
11 https://www.youtube.com/durgasoftware
6) d[ch]=d.get(ch,0)+1
7) for k,v in sorted(d.items()):
8) print('{} occurrs {} times'.format(k,v))
D:\durgaclasses>py test.py
Enter some string to search for vowels:DURGASOFTWARESOLUTIONS
A occurrs 2 times
E occurrs 1 times
I occurrs 1 times
O occurrs 3 times
U occurrs 2 times
D:\durgaclasses>py test.py
Enter some string to search for vowels:mississippi
i occurrs 4 times
Q19) Write a program to check whether the given two strings are
anagrams or not?
Two strings are said to be anagrams iff both are having same content irrespective of
characters position.
Output:
D:\durgaclasses>py test.py
Enter first string:lazy
Enter second string:zaly
The strings are anagrams.
D:\durgaclasses>py test.py
Enter first string:durga
Enter second string:urgadd
The strings aren't anagrams.
12 https://www.youtube.com/durgasoftware
Q20) Write a program to check whether the given string is palindrome
or not ?
A string is said to be palindrome iff original string and its reversed strings are equal.
D:\durgaclasses>py test.py
Enter Some string:level
The given string is palindrome
D:\durgaclasses>py test.py
Enter Some string:madam
The given string is palindrome
D:\durgaclasses>py test.py
Enter Some string:apple
The given string is not palindrome
13 https://www.youtube.com/durgasoftware
16) if j<len(s2):
17) output=output+s2[j]
18) j=j+1
19) if k<len(s3):
20) output=output+s3[k]
21) k=k+1
22) print(output)
Output:
ax1
by2
cz3
d4
e5
f
g
14 https://www.youtube.com/durgasoftware