๐Ÿ“ Python

Python Variables and Strings: Regular, f-, and r-Strings ๐Ÿ“ฆ

P
Author
PyLand Team
๐Ÿ“…
Published
30.03.2026
โฑ๏ธ
Reading time
2 min
๐Ÿ‘๏ธ
Views
397
๐ŸŒฑ
Level
Beginner

A variable is a name associated with a value. Variables let you store data, reuse it, and change it without rewriting the whole program.

Creating a Variable

The = operator assigns a value to a name:

name = 'Alex'
age = 16
height = 1.72
is_student = True

Python determines each value type automatically:

print(type(name))        # str
print(type(age))         # int
print(type(height))      # float
print(type(is_student))  # bool

Using and Updating Values

score = 0
print(score)  # 0

score = 10
score += 5
print(score)  # 15

A new assignment replaces the previous value. score += 5 is shorthand for score = score + 5.

Naming Rules

A variable name:

  • starts with a letter or _;
  • contains letters, digits, and _;
  • contains no spaces or hyphens;
  • is not a Python keyword;
  • is case-sensitive: age and Age are different names.

Use descriptive snake_case names:

user_name = 'Maria'
total_score = 100
rocket_speed = 25

Constants that should not change by convention use UPPER_CASE:

MAX_ATTEMPTS = 3
SECONDS_IN_MINUTE = 60

Never store real passwords, tokens, or API keys directly in source code.

Regular Strings

A regular string stores text:

message = 'Launch ready'
city = "Tbilisi"

Single and double quotes are equivalent. The opening and closing quote styles must match.

f-Strings: Insert Values

The f prefix lets you insert variables and expressions inside {}:

pilot = 'Alex'
altitude = 120

status = f'Pilot {pilot}, altitude {altitude} km'
next_status = f'In one minute: {altitude + 10} km'

print(status)
print(next_status)

Without f, braces remain ordinary text.

r-Strings: Preserve Backslashes

The r prefix creates a raw string. Backslashes do not become \n, \t, or other escape sequences:

project_path = r'C:\Users\student\python-course'
print(project_path)

r-strings are useful for paths, regular expressions, and ASCII art. They cannot end with an odd number of backslashes immediately before the closing quote.

Multiline Strings

Triple single or double quotes preserve line breaks:

mission = """Check systems
Start engines
Reach orbit"""

print(mission)

Prefixes can be combined. For example, fr"..." inserts values while preserving backslashes:

user_name = 'student'
folder = fr'C:\Users\{user_name}\project'
print(folder)

Common Mistakes

# NameError: used before assignment
print(level)
level = 1

# Prints the word name, not the variable value
name = 'Anna'
print('name')

# These are different variables
score = 10
Score = 20

Create a variable before using it, remove quotes around its name when printing its value, and respect capitalization.

Mini Practice

  1. Create name, city, and age variables.
  2. Build a greeting with an f-string.
  3. Store a path containing backslashes in an r-string.
  4. Display both results with print().

Summary

  • name = value creates or updates a variable;
  • descriptive names use snake_case;
  • a regular string stores text;
  • an f-string inserts values and evaluates expressions;
  • an r-string preserves backslashes;
  • triple quotes store multiline text.

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

What is an ORM

ORM (Object-Relational Mapping) is a technology that lets you work with a database through Python...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 364
๐Ÿ“

JSON: Persisting Data

Goal: Learn to save and load data in JSON format.

๐Ÿ“… 03.04.2026 ๐Ÿ‘๏ธ 468
๐Ÿ“

CSV: Working with Tables

CSV (Comma-Separated Values) is a text format for storing tabular data. It opens in Excel...

๐Ÿ“… 03.04.2026 ๐Ÿ‘๏ธ 436