How to Add a New Line in Python Strings?

Adding a new line in Python strings is straightforward using the newline character \n. This character tells Python to start a new line at that point in the string.

Using the Newline Character

print('Hello\nWorld')
# Output:
# Hello
# World

Multiline Strings

Use triple quotes for multiline strings:

message = '''
This is a
multiline string.
'''
print(message)

Using print() Function

Print multiple lines without newline characters:

print('Hello')
print('World')

Conclusion

Adding new lines in Python strings enhances readability and formatting. The \n character is a simple way to achieve this.

Leave a Reply

Your email address will not be published. Required fields are marked *