Episode 4 of 29

Strings in Python

Master working with text strings — creation, indexing, slicing, and common string methods.

Strings are sequences of characters and one of the most used data types.

Creating Strings

name = "Python"
greeting = 'Hello, World!'

String Indexing and Slicing

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

Common String Methods

msg = "Hello, Python!"
print(msg.upper())       # "HELLO, PYTHON!"
print(msg.lower())       # "hello, python!"
print(msg.replace("Python", "World"))
print(len(msg))          # 14

String Concatenation

first = "Hello"
last = "World"
full = first + " " + last  # "Hello World"