In Python, you can use the newline character ("\n") to add a blank line in a string. Methods include: using the join() function to connect the string list using the newline character as a separator; using replace () function, replaces the specified substring with a newline character.
Add empty lines in Python
In Python, you can use the newline character ("\n") to Add empty lines to the string.
String methods
The following methods can add blank lines to the string:
Example
<code class="python"># 使用join() my_string = " ".join(["Hello", "world"]) # 在每个单词之间添加空行 # 使用replace() my_string = my_string.replace(" ", "\n") # 将空格替换为换行符</code>
Output
<code>Hello world</code>
Use print()
You can also use the print() function to add empty lines to the terminal:
<code class="python">print() # 在终端中打印一个空行 print("Hello", "world", sep="\n") # 在单词之间添加空行</code>
Output
<code>Hello world</code>
Use semicolons
Semicolon (;) can print multiple statements on the same line, which can achieve the effect of adding blank lines:
<code class="python">print("Hello"); print("world") # 在语句之间添加空行</code>
Output
<code>Hello world</code>
The above is the detailed content of How to add blank lines in python. For more information, please follow other related articles on the PHP Chinese website!