Q1. What are the characteristics of strings in Python?
Strings are immutable sequences of Unicode characters. They can be indexed and sliced. Many methods are available for manipulation, like upper(), lower(), split(), join(), strip(), replace(), etc. Strings support concatenation and repetition.
Q2. How do you concatenate strings?
Use + operator or join() method. For many strings, join() is more efficient. Example:
s = "Hello" + " " + "World"
words = ["Hello", "World"]
s = " ".join(words)Q3. What is string slicing and how does it work?
Slicing extracts a substring using [start:stop:step]. It returns a new string. Example:
s = "Python"
print(s[2:4]) # "th"
print(s[::-1]) # "nohtyP" Negative indices count from the end.Q4. How do you format strings in Python?
Common ways: f-strings (f"), format() method, and % formatting. f-strings are modern and efficient. Example:
name = "Alice"
age = 30
print(f"{name} is {age} years old")
print("{} is {} years old".format(name, age))Q5. What are the common string methods for checking content?
Methods like isalpha(), isdigit(), isalnum(), isspace(), startswith(), endswith(), find(), index(), count(), etc. Example:
s = "abc123"
print(s.isalnum()) # True
print(s.startswith("ab")) # True