1.
Print length of string
s = input("Enter a string: ")
print("Length:", len(s))
Explanation: len() gives the number of characters in a string.
2. First and last character
s = input("Enter a string: ")
print("First:", s[0])
print("Last:", s[-1])
Explanation: Index 0 gives first char, index -1 gives last char.
3. Reverse string
s = input("Enter a string: ")
print("Reversed:", s[::-1])
Explanation: Slicing with step -1 reverses the string.
4. Palindrome check
s = input("Enter a string: ")
if s == s[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
Explanation: If original string equals reversed string → Palindrome.
5. Count vowels and consonants
s = input("Enter a string: ").lower()
vowels = "aeiou"
v = c = 0
for ch in s:
if [Link]():
if ch in vowels:
v += 1
else:
c += 1
print("Vowels:", v, "Consonants:", c)
Explanation: Loop through characters, check with vowels list, count separately.
6. First 5 characters
s = input("Enter a string: ")
print(s[:5])
Explanation: Slicing [:5] gives first 5 chars.
7. Every second character
s = input("Enter a string: ")
print(s[::2])
Explanation: Slicing with step 2 takes every second char.
8. Remove first and last character
s = input("Enter a string: ")
print(s[1:-1])
Explanation: Slicing [1:-1] removes first and last characters.
9. Characters from index 2 to 7
s = input("Enter a string: ")
print(s[2:8])
Explanation: Slicing [2:8] gives substring between indexes.
10. Reverse a substring
s = input("Enter a string: ")
start = int(input("Start: "))
end = int(input("End: "))
print(s[:start] + s[start:end][::-1] + s[end:])
Explanation: Slice into three parts, reverse middle substring, concatenate.
11. Upper, lower, title
s = input("Enter a string: ")
print([Link]())
print([Link]())
print([Link]())
Explanation: Built-in methods for case conversions.
12. Remove spaces
s = input("Enter a string: ")
print([Link](" ", ""))
Explanation: replace() can remove spaces.
13. Replace 'a' with '@'
s = input("Enter a string: ")
print([Link]("a", "@"))
Explanation: replace() replaces all occurrences.
14. Startswith and endswith
s = input("Enter a string: ")
print([Link]("Hello"))
print([Link]("World"))
Explanation: startswith() and endswith() return boolean.
15. Count word occurrence
s = input("Enter a string: ")
print([Link]("python"))
Explanation: count() returns frequency of substring.
16. Check only digits
s = input("Enter a string: ")
print([Link]())
Explanation: isdigit() checks digits only.
17. Alphanumeric check
s = input("Enter a string: ")
print([Link]())
Explanation: isalnum() checks letters and digits only.
18. Substring without in
s = input("Enter main string: ")
sub = input("Enter substring: ")
if [Link](sub) != -1:
print("Found")
else:
print("Not found")
Explanation: find() returns index or -1 if not found.
19. Index of first 'is'
s = input("Enter a string: ")
print([Link]("is"))
Explanation: find() returns index of first occurrence.
20. All positions of a char
s = input("Enter a string: ")
ch = input("Enter character: ")
for i in range(len(s)):
if s[i] == ch:
print(i, end=" ")
Explanation: Loop with indexes, print positions.
21. Remove duplicates
s = input("Enter a string: ")
result = ""
for ch in s:
if ch not in result:
result += ch
print(result)
Explanation: Keep only first occurrence of each char.
22. Most frequent character
s = input("Enter a string: ")
from collections import Counter
freq = Counter(s)
print(freq.most_common(1)[0])
Explanation: Counter finds frequencies, most_common(1) gives top.
23. First non-repeating char
s = input("Enter a string: ")
from collections import Counter
freq = Counter(s)
for ch in s:
if freq[ch] == 1:
print(ch)
break
Explanation: Check frequency counts, print first unique char.
24. Sort characters alphabetically
s = input("Enter a string: ")
print("".join(sorted(s)))
Explanation: sorted() returns list of chars in order, join back to string.
25. Split into words
s = input("Enter a sentence: ")
print([Link]())
Explanation: split() divides string by spaces.
26. Anagram check
s1 = input("Enter first string: ")
s2 = input("Enter second string: ")
if sorted(s1) == sorted(s2):
print("Anagram")
else:
print("Not Anagram")
Explanation: Anagrams have same sorted letters.
27. Rotation check
s1 = input("Enter first string: ")
s2 = input("Enter second string: ")
if len(s1) == len(s2) and s2 in (s1+s1):
print("Rotation")
else:
print("Not Rotation")
Explanation: Rotation will always be substring of string+string.
28. Remove vowels
s = input("Enter a string: ")
vowels = "aeiouAEIOU"
result = "".join([ch for ch in s if ch not in vowels])
print(result)
Explanation: Filter out vowels using list comprehension.
29. Replace spaces with -
s = input("Enter a string: ")
print([Link](" ", "-"))
Explanation: replace() to swap characters.
30. Longest word in sentence
s = input("Enter a sentence: ")
words = [Link]()
longest = max(words, key=len)
print("Longest word:", longest)
Explanation: split into words, max by length.
31. Password validation
import re
pwd = input("Enter password: ")
pattern = r"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&]).{8,}$"
if [Link](pattern, pwd):
print("Valid password")
else:
print("Invalid password")
Explanation: Regex ensures rules: min 8, upper, lower, digit, special char.
32. Count words
s = input("Enter a sentence: ")
print("Words:", len([Link]()))
Explanation: split() divides by spaces, len counts words.
33. Capitalize first letter of each word (manual)
s = input("Enter a sentence: ")
words = [Link]()
cap = [w[0].upper() + w[1:] for w in words]
print(" ".join(cap))
Explanation: Manually capitalize by slicing and upper().
34. Caesar Cipher encode (+2)
s = input("Enter string: ")
result = ""
for ch in s:
if [Link]():
base = ord("A") if [Link]() else ord("a")
result += chr((ord(ch)-base+2)%26+base)
else:
result += ch
print(result)
Explanation: Shift letters by 2 in alphabet, wrap with modulo.
35. Caesar Cipher decode (-2)
s = input("Enter encoded string: ")
result = ""
for ch in s:
if [Link]():
base = ord("A") if [Link]() else ord("a")
result += chr((ord(ch)-base-2)%26+base)
else:
result += ch
print(result)
Explanation: Shift letters back by 2 to decode.