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

Python string programs

The document contains several Python programs demonstrating string manipulation techniques. It covers topics such as traversing strings with loops, creating and concatenating strings, string slicing, mutable strings, searching for character positions, and counting occurrences of a letter in a string. Each program includes user input and prints the results accordingly.

Uploaded by

riaz ahamed
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)
5 views

Python string programs

The document contains several Python programs demonstrating string manipulation techniques. It covers topics such as traversing strings with loops, creating and concatenating strings, string slicing, mutable strings, searching for character positions, and counting occurrences of a letter in a string. Each program includes user input and prints the results accordingly.

Uploaded by

riaz ahamed
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/ 2

1.

Python program for traversal with for loop


fruit = input("Enter a fruit name:")
for char in fruit:
print(char, end=' ')

2. Python program to create, concatenate and print a string and accessing sub-
string from a given string.

s1=input("Enter first String : ");


s2=input("Enter second String : ");
print("First string is : ",s1);
print("Second string is : ",s2);
print("concatenations of two strings :",s1+s2);
print("Substring of given string :",s1[1:4]);

3. Python program for String Slicing


text = "Hello World"
b = text[1:4]
print(b)
print(text[2:])
print(text[:2])
start = 1
end = 4
print(text[start:end])

4. Python program for mutable String


greeting = 'Hello, world!'
new_greeting = 'J' + greeting[1:]
print(new_greeting)

5. Python program for searching the character position in a string


def find(word, letter):
index = 0
while index < len(word):
if word[index] == letter:
return index
index = index + 1
return -1

word = input("Enter a word:")


search=input("Enter a letter to search:")
c=find(word, search)
if c==-1:
print("Letter is not found in search")
else:
print("The index of search letter",search, "is in",c, "position")

6. Python program for search a String and count

word = input("Enter a word:")


search=input("Enter a letter to count:")
count = 0
for letter in word:
if letter == search:
count = count + 1
else:
print("Searching letter is not in the word")
break

print(count)

You might also like