简体   繁体   English

如何在 Python 中的多行中添加多个字符串?

[英]How do I add multiple strings over several lines in Python?

I'm lost in Python world:我迷失在 Python 世界:

message = struct.pack('B', 4) +
    minissdpdStringEncode(st) +
    minissdpdStringEncode(usn) +
    minissdpdStringEncode(server) +
    minissdpdStringEncode(location)

It doesn't run.它不运行。 Do I really need to put this all on one line or something?我真的需要把这一切都放在一条线上吗?

That would be messy in my opinion.在我看来,那会很混乱。

You have two choices:你有两个选择:

message = struct.pack('B', 4) + \
    minissdpdStringEncode(st)

or或者

message = (struct.pack('B', 4) +
    minissdpdStringEncode(st))

I usually find the second form with parentheses easier to read.我通常发现带括号的第二种形式更容易阅读。

You can continue a line by ending it with a backslash \\ :您可以通过以反斜杠\\结尾来继续一行:

message = struct.pack('B', 4) + \
    minissdpdStringEncode(st) + \
    minissdpdStringEncode(usn) + \
    minissdpdStringEncode(server) + \
    minissdpdStringEncode(location)

除了最后一行之外,在语句的每一行末尾添加一个反斜杠 (\\)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python 将 CSV 中的重复项添加到多个实例的特定行的末尾? - How do I add duplicates from a CSV to the end of a specific row over several instances using Python? 如何将长字符串的定义拆分为多行? - How do I split the definition of a long string over multiple lines? 如何在python中添加多个列表的元素 - How do I add the elements of several lists in python 如何在python中几个文件的某些行的末尾添加一个字符串 - how to add a string at the end of some lines of several files in python Python:如何一次浏览文件中的多行? - Python: How do I go through multiple lines in a file at once? 如何在文件中搜索字符串并将其替换为Python中的多行? - How do I search a file for a string and replace it with multiple lines in Python? 如何将文件中的多行数据读入python? - How do I read multiple lines of data from a file into python? 我如何在 html 中运行多行 python - how do i run multiple lines of python in html 如何对具有多个字符串的 .csv 列进行排序(Python) - How do I sort through .csv columns with multiple strings (Python) 如何在 python 的多个字符串中找到相似的内容? - How do I find what is similar in multiple strings in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM