Python strings
// updated 2025-11-06 19:02
Strings are simply plain text that can consist of letters, digits, symbols (aka "special" characters) and blank spaces:
tagline = "developer and explorer since 2000"While we don't have to assign a variable to a string, using a variable gives us a compact way to manipulate strings, e.g.:
# string indexing
print(tagline[0] + tagline[1] + tagline[2])
# Output:
# dev
# string index ranges [start:number_of_characters]
print(tagline[0:7])
print(tagline[2:6])
print(tagline[-3:]) # get last 3 characters
# Output:
# develop
# velo
# 000String methods
Built-in functions for strings in Python include things like:
len()= to determine the length of a stringlower()= to convert all characters to lowercaseupper()= to convert all characters to uppercasetitle()= to capitalize the first letter of each wordstrip()= remove any instances of whitespace at the start and end of a stringstrip(character)= remove any instances of a character at the start and end of a stringsplit(character)= splits the string into a list of strings, delimited by a character (or blank space if no character specified)find(character)= the index of the first instance of the character's appearance in the string
Also, we may notice that if our string contains quotation marks, we would need to escape them with a backslash (\) before each quotation mark or else the sky will fall on the code!
Some examples:
tagline = " ...***...welcome to Joncoded...***... "
print(len(tagline))
# 40
print(tagline.lower())
# ...***...welcome to joncoded...***...
print(tagline.upper())
# ...***...WELCOME TO JONCODED...***...
print(tagline.title())
# ...***...Welcome To Joncoded...***...
print(tagline.strip())
# ...***...welcome to Joncoded...***...
print(tagline.split())
# [' ...***...welcome', 'to', 'Joncoded...***... ']
print(tagline.split('***'))
# [' ...', '...welcome to Joncoded...', '... ']
tagline2 = "wwww-welcome to joncoded-"
print(tagline2.find("-"))
# 4
print(tagline2.replace('joncoded', 'joecoded'))
# wwww-welcome to joecoded-
print(tagline2.strip('w'))
# -welcome to joncoded-
tagline3 = "I need \"these quotation marks\"!"
# I need "these quotation marks"!
The in syntax with strings
We can see if a substring exists within a string using the in keyword:
name = "Toronto"
print("Tor" in name)
# True
print("x" in name)
# FalseAnother use of the keyword serves to iterate through the string by each character:
name = "Toronto"
for x in name:
print(x)
# Output:
# T
# o
# r
# o
# n
# t
# oString index errors
If we try to access an index of a string for a character that does not exist, the code will create an IndexError:
name = "Toronto"
print(name[7])
# IndexError, as the last character has an index of 6
# recall that indices begin with 0 in PythonString immutability
Also, we cannot edit a string once we define them:
name = "Toronto"
name[0] = "K"
print(name)
# TypeError: 'str' object does not support item assignmenetWe can remove or extract characters from a string variable but that would almost always involve defining a new variable to hold the new string!
name = "Toronto"
new_name = name[2:]
print("Tka" + new_name)
# Tkaronto