Beginner Fundamentals

Strings

Strings are text, wrapped in single or double quotes.

a = "hi"
b = 'world'

Multiple lines

text = """line 1
line 2"""

Access characters (index and slice)

s = "Python"
print(s[0])     # P
print(s[-1])    # n
print(s[0:3])   # Pyt
print(s[2:])    # thon

Useful methods

s = "Hello World"
print(s.upper())        # HELLO WORLD
print(s.lower())        # hello world
print(s.replace("World", "Python"))
print(s.split(" "))     # ['Hello', 'World']
print(len(s))           # 11
print("World" in s)     # True

Formatting with f-strings

The modern way to build text with variables:

name = "Ana"
age = 30
print(f"{name} is {age} years old")