Skip to content
C

Python Interview Questions

Strings Interview Questions

Python's immutable string type -- formatting, splitting, joining, and Unicode handling.

Question 1: What is a string?

Ans

A Python string is an immutable sequence of Unicode characters. It supports indexing, slicing, searching, formatting, and many useful methods.

Example

python
name = "Python" print(name[0], name[-1])

Important Point

String operations that appear to modify a string create or return another string.

Question 2: Why are strings immutable?

Ans

String immutability means an existing string object's characters cannot be changed. This gives predictable value semantics and allows strings to be safely shared and hashed.

Example

python
s = "cat" s = s + "s" print(s)

Important Point

Concatenating many strings repeatedly in a loop can create unnecessary intermediate objects; use a list and `join()` for many pieces.

Question 3: What is f-string?

Ans

An f-string is a formatted string literal that evaluates expressions inside {} and produces readable formatted output.

Example

python
name = "Amit" age = 25 print(f"{name} is {age} years old")

Important Point

Expressions inside f-strings should be kept simple; complex logic belongs outside the formatting expression.

Question 4: format() vs f-string?

Ans

Both support formatted strings, but f-strings are usually more concise when formatting values already available in the current scope.

Example

python
name = "Amit" print(f"Hello {name}") print("Hello {}".format(name))

Important Point

Choose the style supported by the project's Python version and coding conventions.

Question 5: What is split()?

Ans

split() divides a string into a list of substrings using a separator or whitespace rules when no separator is provided.

Example

python
text = "Java Python SQL" print(text.split())

Important Point

Repeated whitespace is handled conveniently when the separator is omitted.

Question 6: What is join()?

Ans

join() combines an iterable of strings into one string using the calling string as the separator.

Example

python
words = ["Python", "is", "easy"] print(" ".join(words))

Important Point

Every element supplied to join must be a string.

Question 7: replace() vs strip()?

Ans

replace() substitutes occurrences of one substring with another. strip() removes specified characters from the beginning and end of a string, not from the middle.

Example

python
s = " Python " print(s.strip()) print(s.replace("Python", "Java"))

Important Point

`strip("ab")` removes any leading/trailing a or b characters, not the exact substring "ab".

Question 8: What is Unicode in Python strings?

Ans

Python 3 strings represent Unicode text, allowing code to work with characters from many writing systems. Encoding becomes important when converting text to bytes for files or networks.

Example

python
text = "नमस्ते" data = text.encode("utf-8") print(data.decode("utf-8"))

Important Point

A Unicode string and encoded bytes are different types.

Continue Your Preparation