Strings (and Chars)

This means that each char has its position in a string and can be retrived by accessing its index (its numerical position in the string in this case).

# Strings are sequence of chars
my_var = "Hello World!"
letter = my_var[i]
print(letter)


Indexing


You can manipulate string in multiple ways using indexing methods. Here’s just some of the most current.

# You can access multiple chars at once
my_var = "Hello World!"
letters = my_var[:i]
print(letters)


Length


It is possible also to obtain the length (the number of chars) in a string by calling the function A len().

# You can access the string length
# with the method len()
my_var = "Hello World!"
length = len(my_var)
print(length)


Split


Strings can also be seperated by a char.

# You can split strings on a character
my_var = "Hello World!"
split_var = my_var.split(' ')
print(split_var)


Merging


And they can be merged together into one new string variable.

# You can sum strings
a = "Hello"
b = " "
c = "World!"
print(a+b+c)


🛠 Exercises


01: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍 02: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍 03: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍 04: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍

Solutions:

01: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍 02: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍 03: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍 04: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍