
In ECET 2026 Computer Science, Python String Functions & Formatting is one of the most repeated topics. Questions are mostly direct (syntax-based, outputs, or theory). By mastering string functions, you can easily score 2–3 marks in Python programming section.
⚙️ Concept Notes – Python String Functions
🔹 What is a String?
- A string is a sequence of characters enclosed within single (
' '
), double (" "
), or triple quotes (''' '''
). - Strings in Python are immutable (cannot be changed once created).
🔹 Common String Functions with Examples
- len() → Returns length of a string
s = "Python"
print(len(s)) # Output: 6
- upper() → Converts all characters to uppercase
s = "hello"
print(s.upper()) # Output: HELLO<br>
- lower() → Converts all characters to lowercase
s = "HELLO"
print(s.lower()) # Output: hello<br>
- title() → Converts first letter of each word to uppercase
s = "python string functions"
print(s.title()) # Output: Python String Functions<br>
- capitalize() → Converts first character to uppercase, rest lowercase
s = "hello world"
print(s.capitalize()) # Output: Hello world<br>
- strip() → Removes spaces from beginning and end
s = " Python "
print(s.strip()) # Output: Python<br>
- replace() → Replaces a substring with another
s = "I like Java"
print(s.replace("Java", "Python")) # Output: I like Python<br>
- find() → Returns index of first occurrence, else -1
s = "Python Programming"
print(s.find("gram")) # Output: 10<br>
- count() → Counts number of occurrences of substring
s = "banana"
print(s.count("a")) # Output: 3<br>
- startswith() / endswith() → Checks prefix or suffix
s = "Python"
print(s.startswith("Py")) # True<br>print(s.endswith("on")) # True<br>
⚙️ String Formatting in Python
- Using format() method
name = "Ravi"
age = 20
print("My name is {} and I am {} years old".format(name, age))
# Output: My name is Ravi and I am 20 years old<br>
- Using f-strings (Python 3.6+)
name = "Ravi"
age = 20
print(f"My name is {name} and I am {age} years old")
# Output: My name is Ravi and I am 20 years old<br>
- Using % formatting
name = "Ravi"
age = 20
print("My name is %s and I am %d years old" % (name, age))
# Output: My name is Ravi and I am 20 years old<br>
🔟 10 Expected MCQs – ECET 2026
Q1. Which function returns length of a string?
A) size()
B) count()
C) len()
D) length()
Q2. "hello".upper()
gives:
A) hello
B) Hello
C) HELLO
D) error
Q3. "Python".find("th")
returns:
A) -1
B) 2
C) 1
D) 3
Q4. "banana".count("a")
gives:
A) 1
B) 2
C) 3
D) 4
Q5. "python".capitalize()
gives:
A) Python
B) PYTHON
C) python
D) Error
Q6. Which method removes spaces at the beginning and end of string?
A) trim()
B) strip()
C) clean()
D) cut()
Q7. f-strings were introduced in:
A) Python 2
B) Python 3.5
C) Python 3.6
D) Python 3.8
Q8. "hello".replace("l", "p")
gives:
A) heppo
B) heppo
C) heppo
D) heppo (repeat check, will adjust)
Q9. "python".startswith("py")
gives:
A) True
B) False
C) 0
D) None
Q10. Which of the following is immutable?
A) List
B) String
C) Dictionary
D) Set
✅ Answer Key
Q.No | Answer |
---|---|
Q1 | C |
Q2 | C |
Q3 | B |
Q4 | C |
Q5 | A |
Q6 | B |
Q7 | C |
Q8 | A |
Q9 | A |
Q10 | B |
🧠 Explanations
- Q1 → C:
len()
returns length. - Q2 → C:
.upper()
converts to uppercase. - Q3 → B: Index of “th” in “Python” is 2.
- Q4 → C: “a” occurs 3 times in “banana”.
- Q5 → A:
.capitalize()
makes first letter uppercase. - Q6 → B:
.strip()
removes spaces. - Q7 → C: f-strings introduced in Python 3.6.
- Q8 → A: Replace “l” with “p” → “heppo”.
- Q9 → A: Yes, “python” starts with “py”.
- Q10 → B: Strings are immutable in Python.
🎯 Why This Practice Matters
String functions are direct, memory-based questions in ECET. Almost every year, 2–3 MCQs are asked. Knowing function names, outputs, and formatting styles ensures guaranteed marks.
📲 Join Our ECET Prep Community
👉 Daily MCQs, Solved Notes, Video Sessions:
Join What’s App @LearnNewThingsHub