Day1: String Manipulation in Python: String Concatenation and New Line Escape Sequence

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:

  1. Escape Sequences: Use \n to add a new line to your strings, making it easier to format multiline outputs.
  2. String Concatenation with Spaces: You can add spaces between words in a string by explicitly concatenating a space " ".

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! ??

要查看或添加评论,请登录

Muhammad Hanzala的更多文章

社区洞察

其他会员也浏览了