0% found this document useful (0 votes)
1 views

String Assignment - Part 1

The document contains a series of Python programming assignments focused on string manipulation. It includes tasks such as calculating string length, extracting characters, reversing strings, counting character types, and merging strings. Each task is accompanied by example inputs and outputs, along with code snippets demonstrating the solutions.

Uploaded by

KINJAL PARMAR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

String Assignment - Part 1

The document contains a series of Python programming assignments focused on string manipulation. It includes tasks such as calculating string length, extracting characters, reversing strings, counting character types, and merging strings. Each task is accompanied by example inputs and outputs, along with code snippets demonstrating the solutions.

Uploaded by

KINJAL PARMAR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

4/25/24, 1:11 AM String Assignment

Write a Python program to calculate the


length of a string without using len()
Input: Komal

Output: 5

In [17]: s = input("Input: ")


l = 0
for i in s:
l += 1
print("Output: ", l)

Output: 5

Write a Python program to get a string


made of the first 2 and last 2 characters of
a given string. If the string length is less
than 2, return the empty string instead.
Input: PythonProgramming

Outut: Pyng

In [23]: s = input("Input: ")


if(len(s)<4):
print("The string length should be minimum 4")
else:
print("Output: ",s[0:2]+s[len(s)-2:len(s)])

Output: Pyng

Write a Python program to get a string


from a given string where all occurrences
of its first char have been changed to @,
except the first char itself.
Input: restart

Output: resta@t

In [29]: s = input("Input: ")


ans = ""

file:///C:/Users/HP 840-G5 i5 8th/Downloads/String Assignment.html 1/8


4/25/24, 1:11 AM String Assignment

for i in range(len(s)):
if i!=0 and s[i]==s[0]:
ans += '@'
else:
ans += s[i]
print("Output: ", ans)

Output: resta@t

Reverse the string in 4 ways


In [1]: #way 1
s = input("Enter the string -")
print(s[::-1])

#way 2
s = input("Enter the string -")
for x in (reversed(s)):
print(x, end = "")

#way 3
s = input("Enter the string -")
print(''.join(reversed(s)))

#way 4
s = input("Enter the string -")
rev = ""
for i in range(len(s)-1, -1,-1):
rev = rev + s[i]
print(rev)

nohtyP
nohtyP
nohtyP
nohtyP

Write a program that find number of


words, spaces, upper case latters, lower
case latters, digits in a given string
In [7]: s = input("Enter the string -")
space = 0
word = 0
upp =0
low=0
digit = 0
for i in s:
if i.isspace():
space += 1
elif i.isupper():

file:///C:/Users/HP 840-G5 i5 8th/Downloads/String Assignment.html 2/8


4/25/24, 1:11 AM String Assignment

upp += 1
elif i.islower():
low += 1
elif i.isdigit():
digit+=1
print("Number of words = ", space + 1, "Spaces = ", space,"Upper case letter = ", u

Number of words = 2 Spaces = 1 Upper case letter = 1 Lower case letter = 10

WAP to reverse the internal contet of


words in a given string
input: Hello Python

output: olleH nohtyP

input: Hello Python

output: olleH nohtyP

In [2]: #way 1
def revWord(s):
return s[::-1]+" "
s = input("Input: ")
l = s.split()
ans=""
for i in l:
ans += revWord(i)
print("Output: ",ans)

#way 2
def revWord(s):
return s[::-1]+" "
s = input("Input:")
target = ""
l = s.split()
for i in l:
target = target + revWord(i)
print("Output: ",target)

Output: olleH nohtyP


Output: olleH nohtyP

WAP to reverse the order of word


input: Hello Python

output: Python Hello

In [3]: s = input("Input: ")


l = s.split()

file:///C:/Users/HP 840-G5 i5 8th/Downloads/String Assignment.html 3/8


4/25/24, 1:11 AM String Assignment

length = len(l)-1
target = ""
while(length>-1):
target = target + l[length] + " "
length -= 1
print("Output: ", target)

Output: Python Hello

WAP to merge two strings without using +


operator
In [8]: s1 = input("Enter first String: ")
s2 = input("Enter secind Strint: ")
i = 0
finalstr=""
while(i<len(s1)):
finalstr = finalstr + s1[i]
i += 1
j = 0
while(j<len(s2)):
finalstr = finalstr + s2[j]
j += 1
print("Output: ",finalstr)

Output: HelloKDL

Write a python program to print characters


at even position and odd position for a
given string
Input: Komal

Characters at even position: Kml

Characters at odd position: oa

In [12]: s = input("Inout: ")


print("Characters at even position:", s[0::2])
print("Characters at odd position:",s[1::2])

Characters at even position: Kml


Characters at odd position: oa

WAP to merge two strings by taking


characters alternatively
Enter first String: 123

file:///C:/Users/HP 840-G5 i5 8th/Downloads/String Assignment.html 4/8


4/25/24, 1:11 AM String Assignment

Enter second String: h

output: 1h23

In [9]: s1 = input("Enter first String - ")


s2 = input("Enter second String - ")
finalstr = ''
i=0
j=0
while i<len(s1) and j < len(s2):
finalstr = finalstr + s1[i] + s2[j]
i += 1
j += 1
if(i != 0):
finalstr += s1[i:]
if(j != 0):
finalstr += s2[j:]
print("Output: ",finalstr)

Output: 1h23

WAP to sort the string first Alphabets then


numbers
Input: h 12 LLL

Output: hLLL12

In [10]: s = input("Input: ")


target = ''
for i in s:
if(i.isalpha()):
target += i
for i in s:
if(i.isdigit()):
target += i
print("Output: ",target)

Output: hLLL12

WAP to do the following:


input : a1b3c4

output : abbbcccc

In [11]: s = input("Input: ")


temp = ''
target = ''
for i in s:
if(i.isalpha()):

file:///C:/Users/HP 840-G5 i5 8th/Downloads/String Assignment.html 5/8


4/25/24, 1:11 AM String Assignment

temp = i
continue
target = target + temp *int(i)
print("Output: ",target)

Output: abbbcccc

Write a python program to remove


duplicate characters from a given input
string
Input: ABCDABDEEEFFG

Output: ABCDEFG

In [13]: s = input("Input: ")


ans = ''
for i in s:
if i not in ans:
ans += i
print("Output: ",ans)

Output: ABCDEFG

Write a python program to return another


string similar to the input string, but with
its case inverted.
Input: Hello WoRld!!

Output: hELLO wOrLD!!

In [16]: s = input("Input: ")


ans = ''
for i in s:
if i.islower():
ans += i.upper()
elif i.isupper():
ans += i.lower()
else:
ans += i
print("Output: ", ans)

Output: hELLO wOrLD!!

Write a Python program to find the first


appearance of the substrings 'not' and
file:///C:/Users/HP 840-G5 i5 8th/Downloads/String Assignment.html 6/8
4/25/24, 1:11 AM String Assignment

'poor' in a given string. If 'not' follows


'poor', replace the whole 'not'...'poor'
substring with 'good'. Return the resulting
string
Input: The lyrics is not that poor

Output: The lyrics is good

Input: The lyrics is poor

Output: The lyrics is poor

In [33]: s = input("Input: ")


lower_s = s.lower();
flag_poor = 0
flag_not = 0
ans=""
if 'not' in lower_s:
flag_not = 1
i_not = s.find('not')
if 'poor' in lower_s:
flag_poor = 1
i_poor = s.find('poor')
if(flag_poor == flag_not == 1):
ans += s[:i_not] + "good"
else:
ans += s[:]
print("Output: ", ans)

Output: The lyrics is poor

Write a Python program to remove the nth


index character from a nonempty string
Input: Python

Index to be removed: 3

Output: Pyton

In [35]: s = input("Input: ")


index = int(input("Index to be removed: "))
ans = ''
for i in range(len(s)):
if i != index:
ans += s[i]
print("Output: ", ans)

file:///C:/Users/HP 840-G5 i5 8th/Downloads/String Assignment.html 7/8


4/25/24, 1:11 AM String Assignment

Output: Pyton

In [ ]:

file:///C:/Users/HP 840-G5 i5 8th/Downloads/String Assignment.html 8/8

You might also like