Python Strings

3 min read ·

In Python, strings are used to store text data. A string is a sequence of characters enclosed in single quotes, double quotes, or triple quotes.
Strings are one of the most commonly used data types in Python.

What Is a String?

A string is a collection of characters such as:
  • Letters
  • Numbers
  • Symbols
  • Spaces

Creating Strings in Python

Strings can be created using:
  • Single quotes (' ')
  • Double quotes (" ")
  • Triple quotes (''' ''' or """ """)

Single Quotes

Double Quotes

Triple Quotes (Multi-line Strings)


Strings Are Immutable

Strings in Python are immutable, which means they cannot be changed after creation.

String Indexing

Each character in a string has an index position starting from 0.

String Slicing

Slicing is used to extract a portion of a string.

String Length

The len() function is used to find the length of a string.

String Concatenation

Strings can be joined using the + operator.

String Repetition

Strings can be repeated using the * operator.

String Formatting

Using Comma

Using f-strings (Recommended)


Common String Methods


Checking Substrings


Exercise

  • Create a string and print its length
  • Extract first three characters
  • Convert a string to uppercase