gpt4 book ai didi

python - 如何在python中访问多行变量的第n行

转载 作者:行者123 更新时间:2023-12-05 06:34:53 25 4
gpt4 key购买 nike

给定多行变量

var="""this is line 1
this is line 2
this is line 3
this is line 4"""

如何将特定行(比如第 2 行)分配给变量?

最佳答案

>>> var="""this is line 1
... this is line 2
... this is line 3
... this is line 4"""
>>>
>>> line3 = var.splitlines()[3 - 1]
>>> line3
'this is line 3'

str.splitlines 将您的多行字符串拆分为不同的行。 n 行将在索引 n - 1 处。

关于python - 如何在python中访问多行变量的第n行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49955894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com