Python Strings

Beginner
8 min read
Updated: Nov 2026
5 examples
str type

Definition and Usage

In Python, a string is text: an object of type str that stores a sequence of Unicode characters—names, messages, file paths, and almost every piece of text a program prints or reads.

You create strings with single quotes, double quotes, or triple quotes for multi-line text. Build messages with f-strings (f"Hi, {name}") or +. Strings are immutable: methods return a new string; they never change the original.

This page teaches what a Python string is. The String Methods index lists every method guide (capitalize(), find(), format(), and more).

Immutable returns.

Write text = text.upper(). Calling text.upper() alone does not update text.

Prefer f-strings.

For everyday messages in Python 3.6+, f"..." is clearer than chaining + or older % formatting.

Python Syntax

Common ways to create and inspect strings:

Python
literal = "Hello, Python!"      # double or single quotes
name    = "Ada"
hello   = f"Hi, {name}!"        # f-string
full    = first + " " + last    # + concatenation
length  = len(text)             # number of characters
ch      = text[0]               # first character (zero-based)
found   = text.find("Py")       # search (method)

Core Ideas

Ideas every Python string tutorial builds on:

IdeaExampleRemember
Literal"Hello" or 'Hello'Single and double quotes are equivalent
f-stringf"Hi, {name}"Embed expressions in braces
Concatenationa + " " + bCreates a new string
Lengthlen(s)Built-in function, not a method
Indexings[0], s[-1]Zero-based; negatives count from the end
Immutabilitys.upper()Returns a new string

Python Examples

Five short programs that define how strings work in a Python tutorial.

Example 1 — Create and print

Assign text and display it with print.

Python
language = "Python"
version = 3

print("Language:", language)
print("Version:", version)
print("Ready to learn strings!")

How It Works

  1. language stores a double-quoted literal.
  2. print can take several values separated by commas.
  3. Spaces between arguments are added automatically.

Example 2 — Quotes and escapes

Quotes inside strings and multi-line text.

Python
quote = 'She said, "Hello!"'
path = "C:\\Users\\Dev\\project"
lines = """Line 1
Line 2"""

print(quote)
print(path)
print(lines)

How It Works

  1. Mix quotes so the inner quote needs no escape.
  2. \\ inserts a backslash in a normal string.
  3. """...""" keeps newlines as written.

Example 3 — f-strings and +

Build messages the modern Python way.

Python
first = "Hello"
last = "Python"
score = 95

greeting = first + ", " + last + "!"
print(greeting)
print(f"{first}, {last}!")
print(f"Score: {score}")

How It Works

  1. + joins string pieces.
  2. f"..." inserts values inside { }.
  3. Numbers convert automatically inside f-strings.

Example 4 — len() and indexing

Measure length and read characters by position.

Python
word = "Hello"

print("length:", len(word))
print("first:", word[0])
print("last:", word[-1])
print("slice:", word[1:4])

How It Works

  1. len is a built-in function.
  2. [0] is the first character; [-1] is the last.
  3. [1:4] slices from index 1 up to (not including) 4.

Example 5 — find(), endswith(), format()

Everyday method calls you will open next on the methods index.

Python
url = "https://codetofun.com/python/string"
file = "main.py"

print(url.find("/python"))
print(file.endswith(".py"))
print("Checked {0} chars".format(len(url)))
find() Tutorial

How It Works

  1. find returns the start index (or -1 if missing).
  2. endswith checks a suffix such as a file extension.
  3. format fills {0} placeholders (f-strings are often clearer).

Next: Python String Methods Index

After you know what a string is, open the method index for full tutorials on every essential str method in this Python series.

Start withWhy
capitalize()First character upper, rest lower
find()Search for a substring
format()Fill template placeholders
isdigit()Validate digit-only text

Full list: Python String Methods index (/python/string/method).

Frequently Asked Questions

A string is a str object that stores a sequence of Unicode characters—names, messages, paths, and more. You create strings with quotes: "Hello" or 'Hello', or with f-strings like f"Hi, {name}".
No. Strings are immutable. Methods like upper() and replace() return a new string. Assign the result if you need to keep it.
An f-string is a formatted string literal: put f before the quotes and embed expressions in braces, for example f"Score: {score}". It is the clearest everyday way to build messages in modern Python.
Use + for a few pieces: full = first + " " + last. For many pieces, prefer "".join(parts) or an f-string.
Practice the examples on this page, then open the String Methods index for capitalize(), find(), format(), and every other method guide in this Python tutorial series.

Browse every string method

Open the methods index for categorized guides with examples and FAQs.

String Methods Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful