Lesson 16 of 70 – Python Strings
23%

Python Strings

A string is a sequence of characters used to store text in Python. Strings can contain letters, numbers, spaces, and special characters.

Note: Strings in Python are written inside single quotes ' ', double quotes " ", or triple quotes ''' ''' and """ """.
What is a String?

A string is a collection or sequence of characters. For example:

name = "Rahul"

print(name)
Output:
Rahul

Here, "Rahul" is a string.

Creating Strings

Python allows you to create strings using single or double quotation marks.

name = 'Rahul'
city = "Patna"

print(name)
print(city)
Output:
Rahul
Patna
Single Quotes

A string can be enclosed in single quotation marks.

message = 'Hello Python'

print(message)
Output:
Hello Python
Double Quotes

A string can also be enclosed in double quotation marks.

message = "Hello Python"

print(message)
Output:
Hello Python

Both single and double quotes can be used to create normal strings.

Triple Quoted Strings

Triple quotes are commonly used for multi-line strings.

message = """Hello
Welcome to
Python"""

print(message)
Output:
Hello
Welcome to
Python
String with Numbers

A string can contain numbers, but they are treated as characters.

code = "12345"

print(code)
print(type(code))
Output:
12345
<class 'str'>
String Length

The len() function returns the number of characters in a string.

name = "Rahul"

print(len(name))
Output:
5

Spaces are also counted as characters.

text = "Hello World"

print(len(text))
Output:
11
String Indexing

Each character in a string has a position called an index. Python starts indexing from 0.

name = "Python"

print(name[0])
print(name[1])
print(name[2])
Output:
P
y
t
Negative Indexing

Python also supports negative indexing. The last character has index -1.

name = "Python"

print(name[-1])
print(name[-2])
print(name[-3])
Output:
n
o
h
String Slicing

String slicing is used to extract a part of a string.

name = "Python"

print(name[0:3])
Output:
Pyt

The ending index is not included.

Slicing from the Beginning
text = "Programming"

print(text[:4])
Output:
Prog

When the starting index is omitted, Python starts from the beginning.

Slicing to the End
text = "Programming"

print(text[4:])
Output:
ramming

When the ending index is omitted, Python continues to the end.

String Concatenation

Concatenation means joining two or more strings. The + operator is used for concatenation.

first = "Hello"
second = "Python"

result = first + " " + second

print(result)
Output:
Hello Python
String Repetition

The * operator can be used to repeat a string.

text = "Python "

print(text * 3)
Output:
Python Python Python
Checking a Character in a String

The in operator can be used to check whether a character or substring exists in a string.

text = "Python"

print("P" in text)
print("z" in text)
Output:
True
False
not in Operator

The not in operator checks whether a value does not exist inside a string.

text = "Python"

print("Java" not in text)
print("Python" not in text)
Output:
True
False
Changing String Case

Python provides methods such as upper() and lower() for changing the case of a string.

text = "Python Programming"

print(text.upper())
print(text.lower())
Output:
PYTHON PROGRAMMING
python programming
Strings are Immutable

Strings in Python are immutable. This means that individual characters of an existing string cannot be changed directly.

text = "Python"

# text[0] = "J"

The above operation is not allowed because strings cannot be modified character by character.

Instead, create a new string:

text = "Python"

text = "J" + text[1:]

print(text)
Output:
Jython
Formatted Strings

F-strings are a convenient way to insert variables into strings.

name = "Rahul"
age = 20

print(f"My name is {name} and I am {age} years old.")
Output:
My name is Rahul and I am 20 years old.
Escape Characters

Escape characters are used to represent special characters inside strings.

Escape Character Meaning
\n New line
\t Tab
\\ Backslash
\" Double quotation mark
\' Single quotation mark
print("Hello\nWorld")
print("Python\tProgramming")
String Comparison

Strings can be compared using comparison operators.

a = "apple"
b = "banana"

print(a == b)
print(a != b)
Output:
False
True
Practical String Example
first_name = "Rahul"
last_name = "Kumar"

full_name = first_name + " " + last_name

print("Full Name:", full_name)
print("Length:", len(full_name))
print("Uppercase:", full_name.upper())
print("Lowercase:", full_name.lower())
Output:
Full Name: Rahul Kumar
Length: 11
Uppercase: RAHUL KUMAR
Lowercase: rahul kumar
Key Points
  • Strings are used to store text.
  • Strings can be created using single or double quotes.
  • Triple quotes can be used for multi-line strings.
  • Python string indexing starts from 0.
  • Negative indexing starts from -1.
  • String slicing is used to extract part of a string.
  • The + operator joins strings.
  • The * operator repeats strings.
  • The len() function returns the length of a string.
  • Strings are immutable in Python.
  • The in and not in operators can search inside strings.
  • F-strings are useful for formatted output.

🧠 Quick Quiz

Question: What is the index of the first character of a Python string?