Python String

In Python, a string is a sequence of characters enclosed in single quotes (‘ ‘) or double quotes (” “).

Note: Strings are immutable means once created, they cannot be changed.

Q: What is the use of a String?

Ans: String is used to represent text.

Note:- strings are sequences of one-character strings.

Q: How to create a String in Python?

Ans: You can create a string by assigning a sequence of characters to a variable.


# Single quotes
name = 'John'

# Double quotes
greeting = "Hello, world!"

Multiline Strings

To create a multiline string, you can use triple quotes (‘ ‘ ‘or”””):


multiline_string = '''This is
a multiline
string.'''

String Indexing and Slicing

Indexing allows you to access individual characters in a string, using a zero-based index. Negative indexing starts from the end.

Slicing allows you to extract a part of the string by specifying a range.


text = "Python"
print(text[0])  # Output: 'P' (first character)
print(text[-1])  # Output: 'n' (last character)
print(text[1:4])  # Output: 'yth' (characters from index 1 to 3)

Common String Methods

Python provides some common methods below:

lower(): It is used to convert the string to lowercase.

upper(): It is used to convert the string to uppercase.

strip(): It is used to remove leading and trailing spaces.

replace(old, new): It is used to replace occurrences of a substring.

split(delimiter): It is used to split the string into a list of substrings.

join(iterable): It is used to join elements of an iterable (like a list) into a single string.


text = "  Hello, Python!  "
print(text.lower())  # Output: "  hello, python!  "
print(text.upper())  # Output: "  HELLO, PYTHON!  "
print(text.strip())  # Output: "Hello, Python!"
print(text.replace("Python", "World"))  # Output: "  Hello, World!  "

String Concatenation

You can concatenate strings using the + operator or join multiple strings with spaces using join().


first_name = "John"
last_name = "Taylor"
full_name = first_name + " " + last_name  # Concatenation
print(full_name)  # Output: "John Taylor"

String Formatting

You can insert variables into strings using several methods:

1. F-strings (Python 3.6+):


name = "John"
age = 35
print(f"My name is {name} and I am {age} years old.")

2. format() method:


print("My name is {} and I am {} years old.".format(name, age))

3. Percent (%) formatting:


print("My name is %s and I am %d years old." % (name, age))

Python String – Questions and Answers

Q 1: What is a string in Python?

Ans: A string is a sequence of characters enclosed in quotes.

Q 2: How do you create a string?

Ans: By using single, double, or triple quotes.

Q 3: Are strings mutable in Python?

Ans: No, Python strings are immutable.

Q 4: How do you access string characters?

Ans: By using indexing, starting from index 0.

Q 5: Can strings be concatenated?

Ans: Yes, strings can be joined using the + operator.

Python String – Objective Questions (MCQs)

Q1. Which of the following is the correct way to create a string in Python?






Q2. What will be the output of the following code?

x = "Python"
print(x[0])






Q3. Which of the following operators is used to concatenate two strings in Python?






Q4. What will be the output of the following code?

x = "Hello World"
print(x.lower())






Q5. Which of the following statements about strings in Python is true?






Related Python String Topics