Count a letter in a sentence, ignoring case.
Take the letter and sentence from user.
s = input("Enter the sentence: ").lower()
letter = input("Enter the letter for counting: ").lower()
count = 0
for ch in s:
if ch == letter:
count = count + 1
print(count)
Remove all spaces from a sentence.
result = ""
for ch in input():
if ch != " ":
result = result + ch
print(result)
Print the first 3 and last 3 characters of a word joined by ... ("programming" → pro...ing)
word = input()
print(word[:3] + "..." + word[-3:])
Keep asking for a username until it starts with ATR, in any case. Ignore extra spaces.
name = input("Username: ").strip()
while not name.upper().startswith("ATR"):
name = input("Try again: ").strip()
print("Accepted")
Check a phrase as a palindrome, ignoring case, spaces and punctuation. ("Never odd or even" → True)
clean = ""
for ch in input().lower():
if ch.isalpha():
clean = clean + ch
print(clean == clean[::-1])
Count vowels and consonants separately. Ignore spaces and punctuation.
v = 0; c = 0
for ch in input().lower():
if ch.isalpha():
if ch in "aeiou":
v = v + 1
else:
c = c + 1
print(v, c)
Reverse a string using a loop, not [::-1].
result = ""
for ch in input():
result = ch + result
print(result)
The input is a full name with exactly one space. Print it as Last, First with proper capitals. ("asha rao" → Rao, Asha)
name = input().strip()
sp = name.find(" ")
print(name[sp + 1:].capitalize() + ", " + name[:sp].capitalize())
Check a password. It’s “Strong” if it has at least 8 characters, a digit and an uppercase letter, otherwise “Weak”.
pw = input()
has_digit = False; has_upper = False
for ch in pw:
if ch.isdigit():
has_digit = True
if ch.isalpha() and ch == ch.upper():
has_upper = True
if len(pw) >= 8 and has_digit and has_upper:
print("Strong")
else:
print("Weak")
Check a palindrome using two index variables moving inward.
w = input(); i = 0; j = len(w) - 1; ok = True
while i < j:
if w[i] != w[j]:
ok = False
break
i = i + 1
j = j - 1
print(ok)
Find the index of the LAST occurrence of a character. Print -1 if it’s absent.
s = input(); target = input()
last = -1
for i in range(len(s)):
if s[i] == target:
last = i
print(last)
Make every vowel uppercase and leave everything else unchanged. ("education" → "EdUcAtIOn")
for ch in input():
if ch in "aeiou":
result = result + ch.upper()
else:
result = result + ch
print(result)
Swap the first and last characters. ("python" → "nythop")
s = input()
print(s[-1] + s[1:-1] + s[0])
Validate roll numbers like ATR24CS041: length 10, starts with ATR, 2 digits, 2 letters, then 3 digits.
r = input().strip()
print(len(r) == 10 and r.startswith("ATR") and r[3:5].isdigit() and r[5:7].isalpha() and r[7:].isdigit())
Check whether a sentence is a pangram, meaning it uses all 26 letters.
s = input().lower(); ok = True
for letter in "abcdefghijklmnopqrstuvwxyz":
if letter not in s:
ok = False
print(ok)
Find the index of the FIRST vowel in a word (-1 if none).
word = input().lower()
pos = -1
for i in range(len(word)):
if word[i] in "aeiou":
pos = i
break
print(pos)
Make characters at even positions uppercase and odd positions lowercase: "python" → "PyThOn"
s = input()
result = ""
for i in range(len(s)):
if i % 2 == 0:
result = result + s[i].upper()
else:
result = result + s[i].lower()
print(result)
Swap the two halves of an EVEN-length string: "abcdef" → "defabc"
s = input()
mid = len(s) // 2
print(s[mid:] + s[:mid])
Valid email if it has exactly one @ and a .
e = input().strip()
at = e.find("@")
print(e.count("@") == 1 and "." in e[at + 1:])
Check whether two words are anagrams, ignoring case. ("Listen", "Silent" → True)
a = input().lower(); b = input().lower()
ok = len(a) == len(b)
for ch in a:
if a.count(ch) != b.count(ch):
ok = False
print(ok)
Print the most frequent vowel in a sentence and how many times it appears
s = input().lower()
best = ""
best_count = 0
for v in "aeiou":
c = s.count(v)
if c > best_count:
best_count = c
best = v
print(best, best_count)
Remove repeated consecutive characters: "aaabbc" → "abc"
s = input()
result = ""
for ch in s:
if result == "" or ch != result[-1]:
result = result + ch
print(result)
Print the middle character of a string (if the length is even, print the middle two)
s = input()
mid = len(s) // 2
if len(s) % 2 == 1:
print(s[mid])
else:
print(s[mid - 1:mid + 1])
Ask for a 4-digit PIN with at most 3 attempts; print “PIN saved!” or “Locked”
attempts = 0
ok = False
while attempts < 3 and not ok:
pin = input("PIN: ").strip()
attempts = attempts + 1
if pin.isdigit() and len(pin) == 4:
ok = True
if ok:
print("PIN saved!")
else:
print("Locked")
Print the first non-repeating character of a string. ("swiss" → w)
s = input(); found = False
for ch in s:
if s.count(ch) == 1:
print(ch)
found = True
break
if not found:
print("None")