Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Tutorial
  3. Python Strings: Creation, Indexing, and Slicing

Core Python · Tutorial 5

Python Strings: Creation, Indexing, and Slicing

Create strings, access characters, take slices, use escapes, and explore common string operations.

Open the compilerPractice exercises
beginner9 min readUpdated November 19, 20264 learning goals
On this page
  1. Triple-quoted strings
  2. Indexing
  3. Slicing
  4. Immutability
  5. Escape sequences
  6. Checking string contents
  7. Practice

What you will learn

  • Create strings with quotes and triple quotes
  • Index and slice strings
  • Use escape sequences and raw strings
  • Explain why strings are immutable
Before this lesson: Python Variables and Data Types

A string is an ordered sequence of Unicode characters. Create one with single or double quotes.

Python
first = 'Python'
second = "ILoveCompiler"
empty = ""

Use the same quote type on both ends. If the text contains an apostrophe, double quotes are convenient: "It's Python".

Triple-quoted strings#

Triple quotes keep line breaks:

Python
message = """Dear learner,

Welcome to ILoveCompiler.
"""
print(message)

They are often used for docstrings and multi-line text.

Indexing#

Positions start at zero. Negative indices count from the end.

Python
word = "Python"

print(word[0])    # P
print(word[1])    # y
print(word[-1])   # n
print(word[-2])   # o

Indexing outside the string raises IndexError. Test the length first when the position is calculated:

Python
print(len(word))  # 6

Slicing#

A slice takes a section with [start:stop:step]. The start is included and the stop is excluded.

Python
word = "programming"

print(word[0:7])    # program
print(word[3:])     # gramming
print(word[:4])     # prog
print(word[::2])    # pormig
print(word[::-1])   # gnimmargorp

Omitting start begins at the start. Omitting stop continues to the end. A negative step walks backwards.

Slices never raise for out-of-range stops

word[0:100] returns the whole string. Indexing word[100] raises an error. Slices are clamped to the available characters.

Immutability#

Strings cannot be changed in place:

Python
word = "Python"
# word[0] = "J"  # TypeError

Build a new string instead:

Python
word = "J" + word[1:]
print(word)
Expected output

Jython

Every string method returns a new string.

Escape sequences#

Backslash introduces special characters:

| Escape | Meaning | | --- | --- | | \n | New line | | \t | Tab | | \\ | Backslash | | \' | Single quote | | \" | Double quote |

Python
print("First line\nSecond line")
print("Name:\tAda")

Prefix a string with r to treat backslashes literally, which is handy for Windows paths and regular expressions:

Python
print(r"C:\Users\ada")

Checking string contents#

Python
text = "Python 3.14"

print("Python" in text)       # True
print(text.startswith("Py"))  # True
print(text.endswith("14"))    # True
print(text.isalpha())         # False: contains a space and digits

The in operator performs a case-sensitive membership test.

Practice#

Given sentence = "Learning Python is fun", print the first word, the last word, the sentence reversed, and the number of characters. Then replace fun with powerful using a string method and print the new sentence.

Previous tutorialPython Numbers and Arithmetic OperatorsNext tutorialPython Type Conversion and Input Values

Related tutorials

beginner

Python Type Conversion and Input Values

Convert between strings, integers, and floats safely, and understand which conversions can fail.

7 min read
beginner

Python List Methods: append, insert, remove, sort, and More

Modify lists with append, extend, insert, remove, pop, sort, reverse, and understand which methods return None.

8 min read

On this page

  1. Triple-quoted strings
  2. Indexing
  3. Slicing
  4. Immutability
  5. Escape sequences
  6. Checking string contents
  7. Practice

Run the examples

Every Python block has a Run in compiler button that opens the code in a new tab.