To assign a long string

Asked 2 years ago, Updated 2 years ago, 38 views

When assigning super long queries in rows to make them look good, As in javascript, using + or backslash (\) is a good idea I don't know if it's a Python way. It doesn't look very good and it's troublesome to keep writing quotes

long_string='some text not important. just garbage to'+
                'illustrate my example';

#or

long_string='some text not important. just garbage to' \
                'illustrate my example';

What is the most Python way to write a string across multiple lines?

python string

2022-09-21 18:35

1 Answers

This method treats all the codes in quotation marks (including the opening letter) as letters Please write it carefully

s = """\
this is a very
long string if I had the
energy to type more and more ..."""

print s

Output:

this is a very
long string if I had the
energy to type more and more ...

Additionally, there are no spaces or opening characters mixed, but it's annoying because you have to tie them in quotes one by one by line.

s = ("this is a very"
      "long string too"
      "for sure ..."
    )
print(s)

Output: this is a verylong string to forure...


2022-09-21 18:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.