Nearby lessons

19 of 108

Python - String Data Type

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Python
Programming

Single Quotes and Double Quotes

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

Both produce the same result.

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Python
Python

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: """ """

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
durga software solutions
durga software solutions

Triple Single Quotes

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Python
Programming

Triple Double Quotes

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Python
Programming

Using Quotes Inside a String

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
This is "Python" class

Another Example

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
This is 'Python' class

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.

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
The "Python Notes" by 'durga' is very helpful
The "Python Notes" by 'durga' is very helpful

Escape Characters in Strings

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
The "Python Notes" by 'durga' is very helpful

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
P
y
n

Index Out of Range

Accessing an index outside the string length raises an error.

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
IndexError: string index out of range

String Slicing

Slicing returns a part of a string.

Syntax:

string[start:end]

Examples of String Slicing

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
ytho
thon
Pyth
Python

String Multiplication

The * operator repeats a string multiple times.

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
PythonPythonPython

len() Function

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
6

Strings are Immutable

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
TypeError

Character Data Type

Python does not have a separate char data type.

A single character is also stored as a string.

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

Common String Operations

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
P
n
yth
PythonPython
6

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

🧠 Test Your Knowledge

8 Questions

Progress: 0 / 8
Keep Going!Python - Type Casting