LAB – 3 : STRINGS (PYTHON PROGRAMS)
1. Read a sentence, create list of words and count occurrence
sentence = input("Enter sentence: ")
words = [Link]()
print("Word list:", words)
w = input("Enter word to count: ")
print("Word count:", [Link](w))
2. Check whether a string is palindrome
s = input("Enter string: ")
if s == s[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
3. Word with maximum length in a string
s = input("Enter string: ")
words = [Link]()
max_word = words[0]
for w in words:
if len(w) > len(max_word):
max_word = w
print("Word with maximum length:", max_word)
4. Middle three characters of odd length string
s = input("Enter string: ")
mid = len(s) // 2
print("Middle characters:", s[mid-1:mid+2])
5. Count lowercase, uppercase, digits and symbols
s = input("Enter string: ")
lower = upper = digit = symbol = 0
for ch in s:
if [Link]():
lower += 1
elif [Link]():
upper += 1
elif [Link]():
digit += 1
else:
symbol += 1
print("Lowercase:", lower)
print("Uppercase:", upper)
print("Digits:", digit)
print("Symbols:", symbol)
6. Caesar Cipher
text = input("Enter text: ")
n = int(input("Enter shift: "))
result = ""
for ch in text:
if [Link]():
result += chr((ord(ch)+n-97)%26+97)
elif [Link]():
result += chr((ord(ch)+n-65)%26+65)
else:
result += ch
print("Encrypted text:", result)
7. Reverse order of words in a sentence
s = input("Enter sentence: ")
words = [Link]()
rev = words[::-1]
print(" ".join(rev))
8. Remove punctuation (without translate)
s = input("Enter text: ")
punc = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
for ch in punc:
s = [Link](ch, "")
print("Without punctuation:", s)
9. Remove punctuation (with translate)
s = input("Enter text: ")
punc = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
table = [Link]("", "", punc)
print("Without punctuation:", [Link](table))
10. Replace all occurrences of first character except first
s = input("Enter string: ")
result = s[0] + s[1:].replace(s[0], '$')
print("Result:", result)