Day1: String Manipulation in Python: String Concatenation and New Line Escape Sequence
How to manipulate strings in Python by using string concatenation and escape sequences like \n to format the output. Here’s a breakdown of what I learned!
?? PAUSE 1: Using \n to Add a New Line
The \n escape sequence is used to insert a new line into a string, which makes formatting text much easier when you need to print multiple lines.
Here’s how you can repeat "Hello world!" on multiple lines:
For Example:
print("Hello world!")
print("Hello world!")
print("Hello world!")
Output:
Hello world!
Hello world!
Hello world!
Example 2:
# Using \n to insert a new line
message = "Hello world!\n\nHello world!\n\nHello world!"
print(message)
Output:
Hello world!
Hello world!
Hello world!
?? PAUSE 2: Adding a Space Between Concatenated Strings
To concatenate two strings with a space between them, you can either add a space within the string or concatenate a space explicitly.
Example:
# Adding space between the strings "Hello" and "Hanzala"
greeting = "Hello" + " " + "Hanzala"
print(greeting)
Output:
Hello Hanzala
Example 2:
# Adding space after the "Hello(space)" word.
greeting = "Hello " + "Hanzala"
print(greeting)
Output:
Hello Hanzala
?? Key Takeaways:
String manipulation is a crucial skill for creating well-formatted outputs in Python. This will come in handy for building user interfaces, generating reports, or even formatting messages for a program.
Stay tuned as I dive deeper into Python during my #100DaysOfCode journey! ??