Skip to content

Module 3 · Variables and Data Types — topic 7 of 15

Strings

An introduction to the str type: writing text with single, double and triple quotes, measuring length, joining and repeating text, and converting other values to strings.

Beginner 12 min read·Lesson 35 of 306 in Python Programming

Learning objectives

  • Create strings with different quote styles
  • Use len(), + and * with strings
  • Convert values to strings for messages

Definition

An introduction to the str type: writing text with single, double and triple quotes, measuring length, joining and repeating text, and converting other values to strings.

Introduction

A string is a sequence of characters: letters, digits, spaces, symbols and emoji. Names, messages, file contents and web pages are all strings. In Python the type is called str.

This lesson covers the essentials you need right away. Module 7 is devoted entirely to strings and their many methods.

Why it matters

Almost every program reads or produces text. Being comfortable creating, measuring and combining strings is necessary before you can build friendly messages, read input or process data files.

Detailed explanation

Strings are written between matching quotes. Single quotes 'hello' and double quotes "hello" are identical; choose the one that avoids escaping, such as "It's ready".

Triple quotes ("""...""" or '''...''') allow text to span several lines.

len(text) returns the number of characters, including spaces.

+ joins (concatenates) strings; * repeats a string a number of times.

str(value) converts other values to their text form, which is required before joining them with +.

An empty string "" has length 0 and counts as False in conditions.

Strings are sequences, so individual characters can be read by position, which you will explore in Module 7.

Python 3 strings are Unicode, so they can hold text in any language. "नमस्ते", "வணக்கம்" and "こんにちは" are all ordinary strings, and len() counts characters, not bytes. This matters for Indian-language content and for names that include accents.

Strings can be compared with == and ordered with < and >. Comparison is character by character using each character’s code, which is why "apple" < "banana" is True but "Zebra" < "apple" is also True: capital letters come before lower-case ones.

Syntax

Python
name = 'Asha'
message = "It's raining"
note = """Line one
Line two"""
  • Single or double quotes for one-line text.
  • Use the other kind of quote when the text contains one.
  • Triple quotes for multi-line text.

Code examples

Length, joining and repeating

Python
word = "Python"
print(len(word))
print(word + " 3")
print("-" * 10)
Output
6
Python 3
----------

Quotes inside strings

Python
print("It's simple")
print('She said "hi"')
Output
It's simple
She said "hi"

More examples

Building a receipt line

Python
item = "Notebook"
qty = 3
price = 45
line = item + " x" + str(qty) + " = Rs " + str(qty * price)
print(line)
print("Characters:", len(line))
print("=" * len(line))
Output
Notebook x3 = Rs 135
Characters: 20
====================

Step-by-step explanation

  • str(qty) and str(qty * price) convert numbers to text so + can join them.
  • line is built from five pieces into one string.
  • len(line) counts all 20 characters, including spaces.
  • "=" * len(line) draws an underline exactly as wide as the text.

Expected output

Running “Building a receipt line” prints exactly:

TextOutput
Notebook x3 = Rs 135
Characters: 20
====================

Practical applications

  • Every message shown in an app is a string, often built from data.
  • Log files and CSV files are read into programs as strings.
  • Web servers send HTML pages, which are long strings.

Best practices

  • Pick the quote style that avoids escapes.
  • Use f-strings (Module 7) instead of many + joins for readability.
  • Use len() rather than counting characters by hand.

Common mistakes

Mismatched quotes

Avoid

text = 'Hello"

Prefer

text = 'Hello'

A string must end with the same quote it started with.

Joining a number with +

Avoid

print("Qty: " + 3)

Prefer

print("Qty: " + str(3))

+ joins strings only; convert the number with str() or use an f-string.

Key takeaways

  • str holds text inside quotes; triple quotes span lines.
  • len() measures length; + joins; * repeats.
  • Convert other values with str() before joining.

Try it yourself

  1. 1
    Print your name surrounded by a box of * characters whose width matches the name.
  2. 2
    Store a two-line address in one triple-quoted string and print it.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

12 questions cover the “Variables and Data Types” module. Answers and explanations are revealed after you submit.

  • BeginnerWhat happens after x = 10 and then x = "ten"?
  • BeginnerWhat is printed?
  • BeginnerWhat is printed?
Practise this module