阅读Python中的每个单词和行的其余部分?

发布于 2024-12-28 02:30:22 字数 93 浏览 3 评论 0原文

举例来说,我有字符串'20 30 i love you'。如何将 20 和 30 存储到它们自己的变量中,并将字符串的其余部分存储到第三个变量中?

Say for example that I had the string '20 30 i love you'. How can I store 20 and 30 into their own variables and the rest of the string into a third variable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

皇甫轩 2025-01-04 02:30:22
a, b, rest = '20 30 i love you'.split(None, 2)
a, b, rest = '20 30 i love you'.split(None, 2)
╭⌒浅淡时光〆 2025-01-04 02:30:22
line = '20 30 i love you'.split()
a = int(line[0])
b = int(line[1])
word_list = line[2:]

如果您希望将其作为单个字符串而不是单词列表,请从 word_list 中创建一个字符串。

text = ''.join(word_list)
line = '20 30 i love you'.split()
a = int(line[0])
b = int(line[1])
word_list = line[2:]

Make a string out of the word_list if you want it as a single string instead of a list of words.

text = ''.join(word_list)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文