SyntaxStudy
Sign Up
Python String Creation & Indexing
Python Beginner 7 min read

String Creation & Indexing

String Creation & Indexing

Strings in Python are sequences of characters. You can create them with single quotes, double quotes, or triple quotes. Every character has a numeric index starting from 0.

Creating Strings

s1 = 'Hello'
s2 = "World"
s3 = '''Triple
quoted'''

Indexing

word = "Python"
print(word[0])   # P
print(word[3])   # h

Negative Indexing

print(word[-1])  # n (last)
print(word[-2])  # o

Slicing

print(word[0:3])   # Pyt
print(word[2:])    # thon
print(word[:4])    # Pyth
print(word[::2])   # Pto (every 2nd char)
print(word[::-1])  # nohtyP (reversed)
Example
word = "Python"
print(word[0])    # P
print(word[-1])   # n
print(word[1:4])  # yth
print(word[::-1]) # nohtyP
Pro Tip

Strings are immutable — you cannot change a character in place. Create a new string instead.