Problems on Variable and Strings
1. Print Name and Age
Write a program that stores your name and age in variables and prints them in a single line.
Example Output:
My name is Alice and I am 25 years old.
2. Swap Two Variables
Write a program to swap the values of two variables a
and b
.
Example:
Input: a = 10, b = 20
Output: a = 20, b = 10
3. String Concatenation
Given two strings first
and last
, join them with a space between.
Example:
Input: first = "Data", last = "Science"
Output: "Data Science"
4. Find String Length
Write a program that takes a string s
and prints its length.
Example:
Input: "Python"
Output: 6
5. Extract Substring
Given a string word = "datascience"
, print only "data"
.
Expected Output:
data
6. Convert to Uppercase
Take a string input and print it in uppercase.
Example:
Input: "hello world"
Output: "HELLO WORLD"
7. Count Occurrences of a Character
Write a program that counts how many times 'a'
appears in a string.
Example:
Input: "banana"
Output: 3
8. Replace a Word
Given a sentence, replace "apple"
with "orange"
.
Example:
Input: "I like apple"
Output: "I like orange"
9. Check Substring
Check if the word "code"
exists in a given string.
Example:
Input: "I love to code in Python"
Output: True
10. Reverse a String
Write a program that reverses a string.
Example:
Input: "python"
Output: "nohtyp"
11. Remove Whitespace
Remove all leading and trailing spaces from a string.
Example:
Input: " Hello World "
Output: "Hello World"
12. Format String
Given variables name = "Alice"
and score = 95
, print:
Alice scored 95 marks.
13. Repeat String
Write a program to print "ha"
5 times.
Output:
hahahahaha
14. First and Last Character
Write a program to print the first and last characters of a string.
Example:
Input: "Python"
Output: P n
15. Capitalize First Letter
Write a program that converts "hello world"
into "Hello world"
.
Expected Output:
Hello world