1.
Right-align numbers with fill character:
numbers = [23, 456, 7890, 5]
width = 6
fill_char = "#"
for num in numbers:
s = str(num)
print(s.rjust(width, fill_char))
Output:
###23
##456
#7890
#####5
2. Validate product code:
code = "PROD-1234"
print(code.startswith("PROD-") and code[-4:].isdigit())
Output:
True
3. Hyphen to camelCase:
s = "this-is-a-test"
parts = s.split("-")
camel = parts[0] + ''.join(x.capitalize() for x in parts[1:])
print(camel)
Output:
thisIsATest
4. Validate filename:
filename = "document123.txt"
print(filename.startswith("document") and filename.endswith(".txt"))
Output:
True
5. Remove punctuation:
import string
s = "Hello, World! How's it going?"
clean = ''.join(c for c in s if c.isalpha() or c == ' ')
print(clean)
Output:
Hello World Hows it going
6. Find word positions:
sentence = "Find the position of this word"
word = "position"
print(sentence.find(word), sentence.index(word))
Output:
9 9
7. Generate username:
first = "john"
last = "doe"
num = "101"
username = first + "_" + last + "_" + num
print(username)
Output:
john_doe_101
8. Count digits in log:
log = "Error at 10:45PM. Code 403. ID: 8912."
count = sum(c.isdigit() for c in log)
print(count)
Output:
10
9. Split into chunks:
s = "abcdefghij"
n = 3
chunks = [s[i:i+n] for i in range(0, len(s), n)]
print(chunks)
Output:
['abc', 'def', 'ghi', 'j']
10. Center align lines:
lines = ["Hello", "World", "in", "Python"]
width = 20
for line in lines:
print(line.center(width))
Output:
Hello
World
in
Python
11. Align table:
rows = [("Name", "Marks"), ("Alice", "85"), ("Bob", "90")]
width = 10
for row in rows:
print(row[0].ljust(width) + row[1].rjust(width))
Output:
Name Marks
Alice 85
Bob 90
12. Genre check:
genre = "Fantasy"
print(genre.isalpha() and len(genre) >= 5)
Output:
True
13. Count words and numbers:
text = "I have 2 dogs and 3 cats"
words = text.split()
num_count = sum(w.isnumeric() for w in words)
word_count = sum(w.isalpha() for w in words)
print("Words:", word_count, "Numbers:", num_count)
Output:
Words: 5 Numbers: 2
14. Extract quoted text:
s = 'He said "hello" and then "bye"'
result = []
start = s.find('"')
while start != -1:
end = s.find('"', start + 1)
if end == -1:
break
result.append(s[start+1:end])
start = s.find('"', end + 1)
print(result)
Output:
['hello', 'bye']
15. Extract numbers from text:
s = "Patient ID: 123, Room: 45, Dose: 5mg"
nums = ''.join(c if c.isnumeric() else ' ' for c in s).split()
print(nums)
Output:
['123', '45', '5']
16. Words starting with vowels:
s = "An elephant is outside"
vowels = "aeiouAEIOU"
count = sum(word.startswith(tuple(vowels)) for word in s.split())
print(count)
Output:
17. Password validity:
password = "abc123"
has_alpha = any(c.isalpha() for c in password)
has_digit = any(c.isdigit() for c in password)
print(has_alpha and has_digit)
Output:
True
18. Longest digit sequence:
s = "abc12345def678gh"
import re
matches = re.findall(r'\d+', s)
longest = max(matches, key=len) if matches else ""
print(longest)
Output:
12345
19. Join words:
words = ["apple", "banana", "cherry"]
if len(words) == 1:
print(words[0])
else:
print(", ".join(words[:-1]) + " and " + words[-1])
Output:
apple, banana and cherry
20. Count word occurrences:
para = "Python is great. PYTHON is powerful. python rocks."
word = "python"
print(para.lower().count(word.lower()))
Output: