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

Programs Strings

The document contains multiple Python scripts that demonstrate various string manipulation techniques, including reading strings, slicing, checking substring presence, counting characters, and sorting lists. Each script addresses a specific functionality, such as reversing strings, checking string lengths, and finding occurrences of substrings. Overall, the document serves as a comprehensive guide for basic string operations in Python.

Uploaded by

kavya Hanji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Programs Strings

The document contains multiple Python scripts that demonstrate various string manipulation techniques, including reading strings, slicing, checking substring presence, counting characters, and sorting lists. Each script addresses a specific functionality, such as reversing strings, checking string lengths, and finding occurrences of substrings. Overall, the document serves as a comprehensive guide for basic string operations in Python.

Uploaded by

kavya Hanji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

S1.

py

"""

python program to read a string

and print each characters in

forward and reverse directions

"""

s=input("Enter a string:")

print("length of a string:",len(s))

print("Characters in a string in forward directions(first method)")

for i in s:

print(i)

print("Characters in a string in forward directions(second method)")

for i in range(len(s)):

print(s[i])

print("Characters in a string in reverse directions(first method)")

for i in range(-1,-(len(s)+1),-1):

print(s[i])

s2.py

"""

python program to illustrate

string slicing

"""

s="Core python"

print(s[0:9:2])

print(s[::])

print(s[2:4:1])

print(s[::2])

print(s[2::])

print(s[:4:])
print(s[-4:-1])

print(s[-6::])

print(s[-1:-4:-1])

print(s[-1::-1])

s3.py

"""

Read a string and check is len>8 if not

assert the user with message string is not accepted.

Otherwise slice the string from position 3 to 6 and

print that sliced string three time.

"""

s=input("Enter a string:")

assert len(s)>8,"string is not accepted"

s1=s[3:7]

print("sliced string:",s1)

print(s1*3)

s4.py

"""

Read a string and concatenate the slices of string.

The slices of strings are last two characters middle two characters.

Length of a string must be at least six characters

"""

s=input("Enter a string:")

if len(s)>=6:

print("Length of entered string is",len(s))

print(s[-1:-3:-1]+s[len(s)//2:len(s)//2+2])

else:
print("length of the string is less than six")

s5.py

"""

Write a python program to check given substring

is present in main string or not

"""

ms=input("Enter a main string:")

ss=input("Enter a sub string:")

if ss in ms:

print("{} is present in {}".format(ss,ms))

else:

print("{} is not present in main string".format(ss))

s6.py

"""

Write a python program to read two strings

and check are they equal or first string is greater than second or smaller.

Print appropriate messages

"""

s1=input("Enter a first string:")

s2=input("Enter a second string:")

s1.

if s1==s2:

print("both are equal")

elif s1>s2:

print("{} is greater than {}".format(s1,s2))

else:

print("{} is less than {}".format(s1,s2))


s7.py

"""

Write a python program to count space in a string

which are not at beginning and ending of the string

"""

s=' hello how are you '

s=s.rstrip()

#print(s)

count=0

print("String after removing a space:",s)

for i in s:

if i==' ':

count+=1

print("Total number of spaces in: {} are: {}".format(s,count))

s8.py

"""

Python program to find the first and

last occurrence of substring in a given main string using find() and rindex() method

"""

s=input("Enter a main string:")

sb=input("Enter a sub string:")

p=s.find(sb)

if p!=-1:

print("substring is found at {}".format(p))

else:

print("substring is not found")

try:

p1=s.rindex(sb)

print("sub string found at{}".format(p1))

except ValueError:
print("{} is not found".format(sb))

s9.py

"""

Python program to display all positions of a sub string in a given main string

"""

s=input("Enter a main string:")

sb=input("Enter a sub string:")

f=False

pos=-1

n=len(s)

while True:

pos=s.find(sb,pos+1,n)

if pos==-1:

break

print('Found at position:',pos+1)

f=True

if f==False:

print('Not Found')

s10.py

"""

Python program to count number of occurrences of substring

in a main string using count method

"""

s=input('Enter a main string:')

sb=input('Enter a sub string:')

n=s.count(sb,3,len(s))
print("Number of times {} occured is {}".format(sb,n))

s11.py

"""

python program to illustrate immutable property of string

"""

s=input("Enter a string:")

print(s[0])

s[0]='#'

print(s)

s12.py

"""

python program to read a string of numbers separated by space.

Split the string at space. Print the numbers and find their sum

"""

s=input("Enter a string of numbers separted by space:")

l=s.split(' ')

sum=0

for i in l:

print(int(i))

sum=sum+int(i)

print('sum=',sum)

s13.py

"""

Read a list of numbers in a word and join these words

using underscore and print final string

"""
s=eval(input("Enter a list of numbers in a word"))

print("-".join(s))

s14.py

"""

Python program to know the type of character entered by the user.

"""

p=input('Enter a character:')

if p.isalnum():

if p.isalpha():

print("is a alphabetic character")

if p.isupper():

print(" character is upper case letter")

else:

print("lower case letter")

else:

print("It is digit")

elif p.isspace():

print("It is space")

else:

print("It is special character")

s15.py

"""

Python program read a list of n engineering courses

and sort the courses in alphabetical order

"""

n=int(input("Enter a number of courses:"))

courses=[]

for i in range(n):
s=input("Enter a course name:")

courses.append(s)

print("LIST before sorting:",courses)

courses.sort()

print("Courses in sorted order is:")

for i in courses:

print(i)

s16.py

"""

Python program to read a list that has n names.

Search a list for name entered by user.

If present print the position.

"""

n=int(input("Enter a numbers of names to be stored in list:"))

names=[]

for i in range(n):

s=input("Enter a name:")

names.append(s)

k=input('Enter a name to be searched:')

for i in range(n):

if k==names[i]:

print('{} is found in the list at position{}'.format(k,i))

break

else:

print("{} is not found in the list".format(k))


s17.py

"""

Python program to insert a sub string in a string in a particular position

"""

main=input('Enter a main String:')

sub=input('Enter a sub string:')

pos=int(input('Enter a position:'))

final=main[:pos]+sub+main[pos+1:len(main)]

print("final string after inserting a substring is:",final)

You might also like