Nearby lessons

19 of 159

Python - String Data Type

📌 What You Will Learn
  • Define the string data type and how it stores text
  • Create strings using single, double, and triple quotes
  • Access characters using positive and negative indexing
  • Extract parts of a string using slicing
  • Use len() and the * operator with strings
  • Explain why strings are immutable

Introduction

The str data type is used to represent text (strings).

A string is a sequence of characters enclosed in quotes.

You can create strings using:

  • Single quotes (' ')
  • Double quotes (" ")

Example

Loading editor…

Single Quotes and Double Quotes

Single quotes and double quotes are used to create single-line strings.

Both produce the same result.

Loading editor…

Multi-line Strings

Single quotes and double quotes cannot be used to create multi-line strings.

To create multi-line strings, use triple quotes.

How to Define Multi-line String Literals

We can define multi-line string literals by using triple single quotes or triple double quotes.

Triple Single Quotes: ''' '''

Triple Double Quotes: """ """

Loading editor…

Triple Single Quotes

Loading editor…

Triple Double Quotes

Loading editor…

Using Quotes Inside a String

Triple quotes allow you to use both single quotes and double quotes inside a string without escaping them.

Loading editor…

Another Example

Loading editor…

Using Both Single and Double Quotes

Triple quotes are helpful when a string contains both single quotes and double quotes.

Without triple quotes, the string can become invalid and raise a syntax error.

Example of an invalid string that mixes both quote symbols without escape characters:

s = 'The "Python Notes" by 'durga' is very helpful'
# Result: SyntaxError
Loading editor…

Escape Characters in Strings

You can also use the backslash to include quote symbols inside a string.

Loading editor…

String Indexing

Python strings support indexing.

Indexing starts from 0.

Character P y t h o n
Positive Index 0 1 2 3 4 5
Negative Index -6 -5 -4 -3 -2 -1

Accessing Characters

Loading editor…

Index Out of Range

Accessing an index outside the string length raises an error.

Loading editor…

String Slicing

Slicing returns a part of a string.

Syntax:

string[start:end]

Examples of String Slicing

Loading editor…

String Multiplication

The * operator repeats a string multiple times.

Loading editor…

len() Function

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

Loading editor…

Strings are Immutable

Strings are immutable, which means their characters cannot be changed after creation.

Loading editor…

Character Data Type

Python does not have a separate char data type.

A single character is also stored as a string.

Loading editor…

Common String Operations

Loading editor…

Applications of Strings

Strings are widely used to store and process text data.

  • User names
  • Messages
  • Email addresses
  • File names
  • Web content

Important Points

  • The str data type stores text.
  • Strings can be created using single, double, or triple quotes.
  • Strings support indexing and slicing.
  • Python does not have a separate char data type.
  • Strings are immutable.

Key Points

  • The str data type represents text.
  • Strings are sequences of characters.
  • Triple quotes are used for multi-line strings.
  • Triple quotes are also useful when both quote symbols appear in the same string.
  • Use indexing to access characters.
  • Use slicing to extract part of a string.
  • Use len() to find the string length.

Quick Summary

Feature Description
Data Typestr
PurposeStores text data
QuotesSingle, Double, Triple
IndexingSupported
SlicingSupported
MutableNo (Immutable)
Character TypeRepresented using str
📝 Key Takeaways
  • The str data type represents text as a sequence of characters
  • Triple quotes are used to create multi-line strings
  • Indexing starts from 0, and negative indexes access from the end
  • The * operator repeats a string, and len() returns its length
  • Python strings are immutable, and there is no separate char type

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.