Solution on Variable and Strings
🌱 1. Print Name and Age
Problem:
Store your name and age in variables and print them together in a single line.
Example Output:
My name is Alice and I am 25 years old.
Solution:
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
Explanation:
Variables name
and age
store string and integer values.
f-strings
help combine them cleanly without converting numbers to strings manually.
🔁 2. Swap Two Variables
Problem:
Swap values of a
and b
.
Example:
Input: a = 10, b = 20
Output: a = 20, b = 10
Solution:
a = 10
b = 20
a, b = b, a
print(a, b)
Explanation:
Python allows tuple unpacking — a quick and elegant way to swap variable values without a temporary variable.
🧩 3. String Concatenation
Problem:
Join two strings with a space between them.
Example:
Input: "Data", "Science"
Output: "Data Science"
Solution:
first = "Data"
last = "Science"
result = first + " " + last
print(result)
Explanation:
You can combine multiple strings using the +
operator. Always remember to add spaces manually when needed.
🔢 4. Find String Length
Problem:
Find the length of a given string.
Example:
Input: "Python"
Output: 6
Solution:
s = "Python"
print(len(s))
Explanation:
len()
returns the total number of characters in a string, including spaces and punctuation.
✂️ 5. Extract Substring
Problem:
From "datascience"
, extract "data"
.
Solution:
word = "datascience"
print(word[0:4])
Explanation:
Slicing uses the syntax string[start:end]
.
Here, indices 0–3 (data
) are taken, and the end index (4) is excluded.
🔠 6. Convert to Uppercase
Problem:
Convert a string to uppercase.
Example:
Input: "hello world"
Output: "HELLO WORLD"
Solution:
text = "hello world"
print(text.upper())
Explanation:
The .upper()
method converts every alphabetic character to uppercase.
🔎 7. Count Occurrences of a Character
Problem:
Count how many times 'a'
appears in a string.
Example:
Input: "banana"
Output: 3
Solution:
word = "banana"
print(word.count("a"))
Explanation:
The .count()
method counts all non-overlapping occurrences of a substring.
🔁 8. Replace a Word
Problem:
Replace "apple"
with "orange"
in a sentence.
Example:
Input: "I like apple"
Output: "I like orange"
Solution:
text = "I like apple"
new_text = text.replace("apple", "orange")
print(new_text)
Explanation:
.replace(old, new)
substitutes every instance of old
with new
.
🔍 9. Check Substring Presence
Problem:
Check if the word "code"
exists in the string "I love to code in Python"
.
Solution:
sentence = "I love to code in Python"
print("code" in sentence)
Explanation:
The in
operator returns True
if a substring is present in a string, otherwise False
.
🔄 10. Reverse a String
Problem:
Reverse the string "python"
.
Solution:
s = "python"
print(s[::-1])
Explanation:
Using slicing with a step of -1
([::-1]
) returns a reversed copy of the string.
⚙️ 11. Remove Whitespace
Problem:
Remove leading and trailing spaces.
Example:
Input: " Hello World "
Output: "Hello World"
Solution:
text = " Hello World "
print(text.strip())
Explanation:
.strip()
removes spaces (or other whitespace) from both ends of a string.
💬 12. Format String
Problem:
Use variables to print a formatted message.
Example Output:
Alice scored 95 marks.
Solution:
name = "Alice"
score = 95
print(f"{name} scored {score} marks.")
Explanation:
f-strings
make it easy to embed variable values directly inside text.
😂 13. Repeat a String
Problem:
Print "ha"
5 times.
Solution:
print("ha" * 5)
Output:
hahahahaha
Explanation:
Multiplying a string by a number repeats it that many times.
🅰️ 14. First and Last Character
Problem:
Print the first and last character of "Python"
.
Solution:
word = "Python"
print(word[0], word[-1])
Output:
P n
Explanation:
Index 0
refers to the first character, and -1
to the last one.
🧠 15. Capitalize First Letter
Problem:
Convert "hello world"
→ "Hello world"
.
Solution:
text = "hello world"
print(text.capitalize())
Explanation:
.capitalize()
converts only the first letter to uppercase and the rest to lowercase.