Strings
Strings
get_middle_three_chars(“Hyderabad")
get_middle_three_chars(“Bangalore")
Given two strings, s1 and s2. Write a program to create a new string s3 by appending
s2 in the middle of s1.
I/P:
Original Strings are Shriyaan Ram
O/P:
After appending new string in middle: ShriRamyaan
def append_middle(s1, s2):
print("Original Strings are", s1, s2)
mi = int(len(s1) / 2)
x = s1[:mi]
x = x + s2
x = x + s1[mi:]
print("After appending new string in middle:", x)
append_middle(“Shriyaan", “Ram")
Given two strings, s1 and s2, write a program to return a new string made of s1 and
s2’s first, middle, and last characters.
I/P:
s1 = "America"
s2 = "Japan“
O/P: AJrpan
def mix_string(s1, s2):
first_char = s1[0] + s2[0]
middle_char = s1[int(len(s1) / 2)] + s2[int(len(s2) / 2)]
last_char = s1[len(s1) - 1] + s2[len(s2) - 1]
res = first_char + middle_char + last_char
print("Mix String is ", res)
s1 = "America"
s2 = "Japan"
mix_string(s1, s2)
Given string contains a combination of the lower and upper case
letters. Write a program to arrange the characters of a string so that
all lowercase letters should come first.
i/p:
str1 = GPreC
o/p: reGPC
str1 = "GPreC"
print('Original String:', str1)
lower = []
upper = []
for char in str1:
if char.islower():
lower.append(char)
else:
upper.append(char)