← Back to Interactive Learning

Python Basics · Lesson 5

Strings & String Operations

Learn how Python stores and works with text using strings. Practice creating strings, joining text, repeating strings, finding characters and measuring string length.

What you will learn

  • • What a string is
  • • Single and double quotes
  • • String variables
  • • Concatenation with +
  • • String repetition with *
  • • String indexing
  • • Finding string length
  • • Working with spaces and text

Part 1

What is a string?

A string is a sequence of characters used to represent text. Names, cities, product names, messages and email addresses can all be represented using strings in Python.

"Hello"
"Vista Academy"
"Python"
"Dehradun"

CHECK YOUR UNDERSTANDING

Which of the following is a Python string?

Creating strings

Python allows strings to be written using single quotes or double quotes.

name = "Yogesh"
city = 'Dehradun'

print(name)
print(city)

CHECK YOUR UNDERSTANDING

Which two quotation styles can create a basic Python string?

Fill in the Blank

Complete the string: name = _____Python_____

Storing text in variables

Strings can be assigned to variables just like numbers and other Python values.

student = "Rahul"
course = "Python Basics"

print(student)
print(course)

CHECK YOUR UNDERSTANDING

What value is stored in the variable course?

Important operation

Joining strings with +

The + operator can join two strings together. This is called string concatenation.

first = "Vista"
second = "Academy"

result = first + " " + second

print(result)

CHECK YOUR UNDERSTANDING

What will this code print? print("Data" + " Analytics")

Fill in the Blank

Which operator joins two strings together? _____

Watch the spaces

"Hello" + "World"

Result: HelloWorld

"Hello" + " " + "World"

Result: Hello World

CHECK YOUR UNDERSTANDING

What is the output of "Data" + "Science"?

Repeating strings with *

The multiplication operator can repeat a string multiple times.

print("Hi " * 3)

CHECK YOUR UNDERSTANDING

What will "Hi " * 3 produce?

Fill in the Blank

Complete the repetition: "Python" _____ 3

Finding string length

Python's len() function returns the number of characters in a string, including spaces.

name = "Python"

print(len(name))

CHECK YOUR UNDERSTANDING

What is the result of len("Python")?

CHECK YOUR UNDERSTANDING

What is the result of len("Vista Academy")?

Important concept

Accessing characters with indexing

Python strings use indexes to access individual characters. Indexing starts at 0.

word = "Python"

print(word[0])
print(word[1])
print(word[5])
CharacterPython
Index012345

CHECK YOUR UNDERSTANDING

What does "Python"[0] return?

CHECK YOUR UNDERSTANDING

What does "Python"[3] return?

Negative indexing

Python also supports negative indexes. Index -1 refers to the last character.

word = "Python"

print(word[-1])

CHECK YOUR UNDERSTANDING

What does "Python"[-1] return?

Predict the output

Think before running the code

name = "Python"

print(name[1])
print(len(name))

CHECK YOUR UNDERSTANDING

What will the two print statements display?

Strings and numbers are different

Python treats text and numbers differently. You cannot directly concatenate a string and an integer using +.

age = 20

print("Age: " + age)

CHECK YOUR UNDERSTANDING

Why does this code cause a problem?

age = 20

print("Age: " + str(age))

Debug the string code

first_name = "Rahul"
last_name = "Sharma"

full_name = first_name last_name

print(full_name)

CHECK YOUR UNDERSTANDING

What is missing in the full_name expression?

Practice

Complete the code

Fill in the Blank

Complete: greeting = "Hello" _____ " Python"

Fill in the Blank

Complete the function: length = _____(name)

Fill in the Blank

Access the first character: name[_____]

Fill in the Blank

Access the last character: name[_____]

Mini Challenge

Build a student message

Create variables for a student's name and course. Then combine them into one sentence.

name = "Aman"
course = "Python Basics"

message = name + " is learning " + course

print(message)

Expected output:

Aman is learning Python Basics

Final Challenge

Create a simple profile

Create variables for a person's name, city and course. Build a single profile message using string concatenation. Then print the length of the person's name.

name = "Priya"
city = "Dehradun"
course = "Data Analytics"

profile = name + " from " + city + " is learning " + course

print(profile)
print(len(name))

Change the values and create your own profile.

Lesson recap

  • ✓ Strings represent text.
  • ✓ Strings can use single or double quotes.
  • ✓ Strings can be stored in variables.
  • ✓ + joins strings together.
  • ✓ * can repeat strings.
  • ✓ len() returns the number of characters.
  • ✓ String indexes start at 0.
  • ✓ -1 refers to the last character.
  • ✓ Strings and numbers are different data types.