Strings Practice
String manipulation, slicing, formatting and text processing problems.
1
Reverse a String
Easy
Write a program to reverse the given string and print it.
Input Format
A single line containing a string S.
Output Format
Print the reversed string.
Sample Test Cases
|
Sample Input 1
hello |
Sample Output 1
olleh |
|
Sample Input 2
racecar |
Sample Output 2
racecar |
|
Sample Input 3
Python |
Sample Output 3
nohtyP |
2
Count Vowels in a String
Easy
Write a program to count the number of vowels (a, e, i, o, u) in a given string. Case-insensitive.
Input Format
A single line containing a string S.
Output Format
Print a single integer — the number of vowels.
Sample Test Cases
|
Sample Input 1
education |
Sample Output 1
5 |
|
Sample Input 2
Hello |
Sample Output 2
2 |
|
Sample Input 3
Python |
Sample Output 3
1 |
3
Check Palindrome String
Easy
Write a program to check whether the given string reads the same forwards and backwards.
Input Format
A single line containing a string S.
Output Format
Print YES if palindrome, else NO.
Sample Test Cases
|
Sample Input 1
madam |
Sample Output 1
YES |
|
Sample Input 2
racecar |
Sample Output 2
YES |
|
Sample Input 3
hello |
Sample Output 3
NO |
4
Convert to Uppercase
Easy
Write a program to convert the entire given string to uppercase.
Input Format
A single line containing a string S.
Output Format
Print the string in uppercase.
Sample Test Cases
|
Sample Input 1
hello world |
Sample Output 1
HELLO WORLD |
|
Sample Input 2
Tech Bridge |
Sample Output 2
TECH BRIDGE |
|
Sample Input 3
abc123 |
Sample Output 3
ABC123 |
5
Length of String
Easy
Write a program to find and print the length of the given string.
Input Format
A single line containing a string S.
Output Format
Print a single integer — the length of S.
Sample Test Cases
|
Sample Input 1
TechBridge24 |
Sample Output 1
12 |
|
Sample Input 2
Hi |
Sample Output 2
2 |
|
Sample Input 3
Hello World |
Sample Output 3
11 |
6
Remove Spaces
Easy
Write a program to remove all whitespace characters from the given string.
Input Format
A single line containing a string S with spaces.
Output Format
Print the string without any spaces.
Sample Test Cases
|
Sample Input 1
hello world python |
Sample Output 1
helloworldpython |
|
Sample Input 2
a b c |
Sample Output 2
abc |
|
Sample Input 3
python |
Sample Output 3
python |
7
First and Last Character
Easy
Write a program to print the first and last character of the given string on separate lines.
Input Format
A single line containing a string S.
Output Format
Print the first character on line 1 and the last character on line 2.
Sample Test Cases
|
Sample Input 1
python |
Sample Output 1
p n |
|
Sample Input 2
hello |
Sample Output 2
h o |
|
Sample Input 3
ab |
Sample Output 3
a b |
8
Count Words in a String
Easy
Write a program to count the number of words in the given string. Words are separated by single spaces.
Input Format
A single line containing a string S.
Output Format
Print a single integer — the number of words.
Sample Test Cases
|
Sample Input 1
I love python programming |
Sample Output 1
4 |
|
Sample Input 2
one two three |
Sample Output 2
3 |
|
Sample Input 3
python |
Sample Output 3
1 |
9
Check if String Contains Digit
Easy
Write a program to check whether the given string contains at least one digit.
Input Format
A single line containing a string S.
Output Format
Print YES if it contains a digit, else NO.
Sample Test Cases
|
Sample Input 1
python3 |
Sample Output 1
YES |
|
Sample Input 2
hello |
Sample Output 2
NO |
|
Sample Input 3
123 |
Sample Output 3
YES |
10
Swap Case of Characters
Easy
Write a program to swap the case of each character (lower to upper and vice versa) in the given string.
Input Format
A single line containing a string S.
Output Format
Print the string with swapped case.
Sample Test Cases
|
Sample Input 1
HelloWorld |
Sample Output 1
hELLOwORLD |
|
Sample Input 2
PYTHON |
Sample Output 2
python |
|
Sample Input 3
aBc |
Sample Output 3
AbC |
1
Count Each Character Frequency
Medium
Write a program to count the frequency of each character in the given string.
Input Format
A single line containing a string S.
Output Format
Print each character and its count as char: count, one per line.
Sample Test Cases
|
Sample Input 1
abracadabra |
Sample Output 1
a: 5 b: 2 c: 1 d: 1 r: 2 |
|
Sample Input 2
hello |
Sample Output 2
h: 1 e: 1 l: 2 o: 1 |
|
Sample Input 3
aabb |
Sample Output 3
a: 2 b: 2 |
2
Remove Duplicate Characters
Medium
Write a program to remove duplicate characters from the string while preserving the order of first occurrence.
Input Format
A single line containing a string S.
Output Format
Print the string after removing duplicates.
Sample Test Cases
|
Sample Input 1
programming |
Sample Output 1
progamin |
|
Sample Input 2
banana |
Sample Output 2
ban |
|
Sample Input 3
hello |
Sample Output 3
helo |
3
Check Anagram Strings
Medium
Write a program to check whether two strings are anagrams of each other.
Input Format
First line: string A. Second line: string B.
Output Format
Print YES if anagrams, else NO.
Sample Test Cases
|
Sample Input 1
listen |
Sample Output 1
silent |
|
Sample Input 2
listen silent |
Sample Output 2
YES |
|
Sample Input 3
hello world |
Sample Output 3
NO |
Constraints
YES
4
Reverse Words in a String
Medium
Write a program to reverse the order of words in the given sentence.
Input Format
A single line containing a string S with words separated by single spaces.
Output Format
Print the sentence with words reversed.
Sample Test Cases
|
Sample Input 1
I love python |
Sample Output 1
python love I |
|
Sample Input 2
hello world |
Sample Output 2
world hello |
|
Sample Input 3
a b c |
Sample Output 3
c b a |
5
Longest Word in a String
Medium
Write a program to find and print the longest word in the given sentence. If there are multiple, print the first one.
Input Format
A single line containing a string S.
Output Format
Print the longest word.
Sample Test Cases
|
Sample Input 1
python is a powerful language |
Sample Output 1
powerful |
|
Sample Input 2
short and verylong |
Sample Output 2
verylong |
|
Sample Input 3
a bb ccc |
Sample Output 3
ccc |
6
Check Substring Present
Medium
Write a program to check whether a substring is present inside the main string.
Input Format
First line: string S. Second line: substring T.
Output Format
Print YES if T is present in S, else NO.
Sample Test Cases
|
Sample Input 1
Hello World |
Sample Output 1
World |
|
Sample Input 2
Hello World lo Wo |
Sample Output 2
YES |
|
Sample Input 3
abc xyz |
Sample Output 3
NO |
Constraints
YES
7
Count Consonants
Medium
Write a program to count the number of consonants in the given string.
Input Format
A single line containing a string S.
Output Format
Print a single integer — the number of consonants.
Sample Test Cases
|
Sample Input 1
programming |
Sample Output 1
9 |
|
Sample Input 2
hello |
Sample Output 2
3 |
|
Sample Input 3
aeiou |
Sample Output 3
0 |
8
Replace All Spaces with Hyphen
Medium
Write a program to replace every space in the given string with a hyphen (-).
Input Format
A single line containing a string S.
Output Format
Print the modified string.
Sample Test Cases
|
Sample Input 1
hello world python |
Sample Output 1
hello-world-python |
|
Sample Input 2
a b |
Sample Output 2
a-b |
|
Sample Input 3
x y |
Sample Output 3
x--y |
9
Capitalize First Letter of Each Word
Medium
Write a program to capitalize the first letter of every word in the given string.
Input Format
A single line containing a string S.
Output Format
Print the string with each word capitalized.
Sample Test Cases
|
Sample Input 1
python is great |
Sample Output 1
Python Is Great |
|
Sample Input 2
hello world |
Sample Output 2
Hello World |
|
Sample Input 3
a b |
Sample Output 3
A B |
10
Check if String is Pangram
Medium
Write a program to check whether the given string is a pangram (contains every letter a–z at least once).
Input Format
A single line containing a string S.
Output Format
Print YES if pangram, else NO.
Sample Test Cases
|
Sample Input 1
The quick brown fox jumps over the lazy dog |
Sample Output 1
YES |
|
Sample Input 2
hello |
Sample Output 2
NO |
|
Sample Input 3
abcdefghijklmnopqrstuvwxyz |
Sample Output 3
YES |
1
Longest Substring Without Repeating Characters
Hard
Write a program to find the length of the longest substring without repeating characters.
Input Format
A single line containing a string S.
Output Format
Print a single integer — the length of the longest substring.
Sample Test Cases
|
Sample Input 1
abcabcbb |
Sample Output 1
3 |
|
Sample Input 2
bbbbb |
Sample Output 2
1 |
|
Sample Input 3
pwwkew |
Sample Output 3
3 |
2
Minimum Window Substring
Hard
Write a program to find the smallest substring in S that contains all characters of string T.
Input Format
First line: string S. Second line: string T.
Output Format
Print the smallest window substring; print -1 if no such window exists.
Sample Test Cases
|
Sample Input 1
ADOBECODEBANC |
Sample Output 1
ABC |
|
Sample Input 2
a aa |
Sample Output 2
NO |
|
Sample Input 3
abc b |
Sample Output 3
b |
Constraints
BANC
3
String Rotation Check
Hard
Write a program to check whether string B is a rotation of string A.
Input Format
First line: string A. Second line: string B.
Output Format
Print YES if B is a rotation of A, else NO.
Sample Test Cases
|
Sample Input 1
ABCD |
Sample Output 1
CDAB |
|
Sample Input 2
ABCD BCDA |
Sample Output 2
YES |
|
Sample Input 3
ABCD ABCD |
Sample Output 3
YES |
Constraints
YES
4
Longest Palindromic Substring
Hard
Write a program to find the longest palindromic substring in the given string.
Input Format
A single line containing a string S.
Output Format
Print the longest palindromic substring.
Sample Test Cases
|
Sample Input 1
babad |
Sample Output 1
bab |
|
Sample Input 2
cbbd |
Sample Output 2
bb |
|
Sample Input 3
a |
Sample Output 3
a |
5
String to Integer (atoi)
Hard
Write a program to convert the given numeric string to an integer without using int(). Handle optional leading + or - sign.
Input Format
A single line containing a numeric string S.
Output Format
Print the integer value.
Sample Test Cases
|
Sample Input 1
-12345 |
Sample Output 1
-12345 |
|
Sample Input 2
+123 |
Sample Output 2
123 |
|
Sample Input 3
007 |
Sample Output 3
7 |
6
Print All Substrings of a String
Hard
Write a program to print all substrings of the given string, one per line, in order of increasing length.
Input Format
A single line containing a string S.
Output Format
Print all substrings, one per line.
Sample Test Cases
|
Sample Input 1
abc |
Sample Output 1
a b c ab bc abc |
|
Sample Input 2
ab |
Sample Output 2
a b ab |
|
Sample Input 3
xyz |
Sample Output 3
x y z xy yz xyz |
7
Group Anagrams Together
Hard
Write a program to group words that are anagrams of each other from a list of words.
Input Format
First line: integer N. Second line: N space-separated words.
Output Format
Print each group as a space-separated line, groups in order of first appearance.
Sample Test Cases
|
Sample Input 1
5 eat tea tan ate nat |
Sample Output 1
eat tea ate tan nat |
|
Sample Input 2
4 dog god act |
Sample Output 2
dog god act |
|
Sample Input 3
3 cat act tac |
Sample Output 3
cat act tac |
8
First Non-Repeating Character
Hard
Write a program to find the first non-repeating character in the given string.
Input Format
A single line containing a string S.
Output Format
Print the first non-repeating character, or -1 if none exists.
Sample Test Cases
|
Sample Input 1
swiss |
Sample Output 1
w |
|
Sample Input 2
aabbcc |
Sample Output 2
-1 |
|
Sample Input 3
xxyz |
Sample Output 3
y |
9
Compress the String (Run-Length)
Hard
Write a program to compress the string using run-length encoding: char followed by its count.
Input Format
A single line containing a string S.
Output Format
Print the compressed string.
Sample Test Cases
|
Sample Input 1
aaabbcccc |
Sample Output 1
a3b2c4 |
|
Sample Input 2
aab |
Sample Output 2
a2b1 |
|
Sample Input 3
aaa |
Sample Output 3
a3 |
10
Zigzag String Conversion
Hard
Write a program to convert the given string into a zigzag pattern with numRows rows and read it row by row.
Input Format
First line: string S. Second line: integer numRows.
Output Format
Print the zigzag-converted string.
Sample Test Cases
|
Sample Input 1
PAYPALISHIRING |
Sample Output 1
3 |
|
Sample Input 2
AB 1 |
Sample Output 2
AB |
|
Sample Input 3
ABCD 2 |
Sample Output 3
ACBD |
Constraints
PAHNAPLSIIGYIR
Competitive MCQs — Strings
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score
0/ 35
Q1
What is the output of this code?
python
1
2
2
s = "hello"
print(s[0])Correct!
Wrong — correct answer is .
Strings are 0-indexed; s[0] is the first character.
Q2
What is the output of this code?
s = "hello"
print(s[0])
s = "hello"
print(s[0])
Correct!
Wrong — correct answer is .
Strings are 0-indexed; s[0] is the first character.
Q3
Which method converts a string to uppercase?
Correct!
Wrong — correct answer is .
The .upper() method returns an uppercased copy.
Q4
What is the output of this code?
python
1
print(len("Python"))Correct!
Wrong — correct answer is .
len() counts characters: "Python" has 6.
Q5
What is the output of this code?
print(len("Python"))
print(len("Python"))
Correct!
Wrong — correct answer is .
len() counts characters: "Python" has 6.
Q6
Which method removes whitespace from both ends of a string?
Correct!
Wrong — correct answer is .
strip() removes leading/trailing whitespace; trim is Java/JS.
Q7
What is the output of this code?
python
1
2
2
s = "Hello World"
print(s.split())Correct!
Wrong — correct answer is .
split() with no argument splits on whitespace into a list.
Q8
What is the output of this code?
s = "Hello World"
print(s.split())
s = "Hello World"
print(s.split())
Correct!
Wrong — correct answer is .
split() with no argument splits on whitespace into a list.
Q9
Which method replaces a substring in a string?
Correct!
Wrong — correct answer is .
str.replace(old, new) returns a copy with matches replaced.
Q10
What is the output of this code?
python
1
2
2
s = "abcdef"
print(s[1:4])Correct!
Wrong — correct answer is .
Slice [1:4] gives indexes 1, 2, 3 → "bcd".
Q11
What is the output of this code?
s = "abcdef"
print(s[1:4])
s = "abcdef"
print(s[1:4])
Correct!
Wrong — correct answer is .
Slice [1:4] gives indexes 1, 2, 3 → "bcd".
Q12
Which method checks if a string ends with a given suffix?
Correct!
Wrong — correct answer is .
str.endswith(sub) returns True if the string ends with sub.
Q13
What is the output of this code?
python
1
print("5" + "5")Correct!
Wrong — correct answer is .
String concatenation joins the two: "5" + "5" = "55" (a string).
Q14
What is the output of this code?
print("5" + "5")
print("5" + "5")
Correct!
Wrong — correct answer is .
String concatenation joins the two: "5" + "5" = "55" (a string).
Q15
Which expression counts how many times "a" appears in "banana"?
Correct!
Wrong — correct answer is .
count(sub) returns the number of non-overlapping occurrences.
Q16
What is the output of this code?
s = "Hello"
print(s.lower())
s = "Hello"
print(s.lower())
Correct!
Wrong — correct answer is .
lower() returns a lowercase copy of the string.
Q17
What is the output of this code?
s = "Hello World"
print(s[6:])
s = "Hello World"
print(s[6:])
Correct!
Wrong — correct answer is .
Slicing from index 6 to the end gives "World".
Q18
What is the output of this code?
s = "hello"
print(s.capitalize())
s = "hello"
print(s.capitalize())
Correct!
Wrong — correct answer is .
capitalize() uppercases the first character and lowercases the rest.
Q19
Which method checks whether every character is a digit?
Correct!
Wrong — correct answer is .
str.isdigit() returns True when all characters are digits.
Q20
What is the output of this code?
print("123".isdigit())
print("123".isdigit())
Correct!
Wrong — correct answer is .
All three characters are digits, so isdigit() returns True.
Q21
What is the output of this code?
s = " hi "
print(s.strip())
s = " hi "
print(s.strip())
Correct!
Wrong — correct answer is .
strip() removes whitespace from both ends.
Q22
Which method finds the index of a substring or returns -1 if absent?
Correct!
Wrong — correct answer is .
find() returns -1 for missing text; index() would raise ValueError.
Q23
What is the output of this code?
print("hello".find("z"))
print("hello".find("z"))
Correct!
Wrong — correct answer is .
find() returns -1 when the substring is not present.
Q24
What is the output of this code?
print("a-b-c".split("-"))
print("a-b-c".split("-"))
Correct!
Wrong — correct answer is .
split("-") divides the string at each hyphen into three parts.
Q25
Which method joins a list of strings with a separator?
Correct!
Wrong — correct answer is .
The separator string calls join() on the iterable: "-".join([...]).
Q26
What is the output of this code?
print("-".join(["a", "b", "c"]))
print("-".join(["a", "b", "c"]))
Correct!
Wrong — correct answer is .
join places "-" between consecutive list items.
Q27
What is the output of this code?
s = "python"
print(s[::-1])
s = "python"
print(s[::-1])
Correct!
Wrong — correct answer is .
The step -1 reverses the string by slicing from the end.
Q28
What is the output of this code?
print("hello".replace("l", "L"))
print("hello".replace("l", "L"))
Correct!
Wrong — correct answer is .
replace() swaps every occurrence, turning both l characters into L.
Q29
What is the output of this code?
s = "Hello World"
print(s.startswith("He"))
s = "Hello World"
print(s.startswith("He"))
Correct!
Wrong — correct answer is .
startswith() returns True when the string begins with the given prefix.
Q30
What is the output of this code?
print("\t".join(["a", "b"]))
print("\t".join(["a", "b"]))
Correct!
Wrong — correct answer is .
The tab character joins the two strings: "a\tb".
Q31
Which expression returns "WORLD" from s = "hello world"?
Correct!
Wrong — correct answer is .
Slicing the tail and calling upper() gives "WORLD"; the index 11 slice also works.
Q32
What is the output of this code?
s = "abc"
print(s * 2)
s = "abc"
print(s * 2)
Correct!
Wrong — correct answer is .
String repetition duplicates the whole string.
Q33
What is the output of this code?
print(len(""))
print(len(""))
Correct!
Wrong — correct answer is .
An empty string contains zero characters, so its length is 0.
Q34
What is the output of this code?
print("Hello".endswith("lo"))
print("Hello".endswith("lo"))
Correct!
Wrong — correct answer is .
endswith() checks the suffix and returns True here.
Q35
Which of these returns True?
Correct!
Wrong — correct answer is .
isalpha() is True only when letters make up the whole non-empty string.