String Manipulation

Today, we will discuss string manipulation, including slicing and formatting with f-strings.

` ˙⋆˖⁺‧₊☽◯☾₊‧⁺˖⋆˙ `

Escape Sequence

Escape sequences help to manipulate strings format and use some special symbols.

#Example: print("It\'s Wingardium Levi-o-sa,"+ "\nNot Wingardium Levio-saaa!") # Output: # It's Wingardium Levi-o-sa, # Not Wingardium Levio-saaa!

Escape sequences can be placed at any spot of a string.

Slicing Strings

Slicing allows you to extract parts of a string. You can use the slice notation [start:end] to get a substring from the original string.

#Example: Slicing strings text = "Magic Academy" slice1 = text[0:5] # Result: 'Magic' slice2 = text[6:] # Result: 'Academy' print(slice1) print(slice2)

In this example, text[0:5] extracts the first five characters, and text[6:] extracts the characters from position 6 to the end.

Integrating Variables into Strings

You can combine strings and variables to create dynamic messages. This can be done using concatenation or f-strings.

#Example: Integrating variables name = "Draco" age = 17 # Using concatenation message1 = "My name is " + name + " and I am " + str(age) + " years old." # Using f-strings message2 = f"My name is {name} and I am {age} years old." print(message1) print(message2)

In this example, both concatenation and f-strings are used to insert the values of name and age into the string.

Practical Application

In the Hall of Echoes, you will master the art of string manipulation. This involves slicing strings to extract specific parts and using f-strings to create dynamic messages.

⋆˖⁺‧₊ Use slicing to extract a substring from "The Enchanted Forest" and an f-string to greet a mage named "Luna". ₊‧⁺˖⋆

Sample Output:
Enchanted
Welcome, Mage Luna!
#Type your code below: