【问题标题】:How to split a string into a string and an integer? [duplicate]如何将字符串拆分为字符串和整数? [复制]
【发布时间】:2021-12-07 10:07:00
【问题描述】:

如何在python中将一个字符串拆分为多个字符串。我要拆分的字符串格式为:

Variable 1

我想把它分成:

Variable = Variable

Number = 1

【问题讨论】:

  • 你知道字符串有一个叫split的方法吗?
  • 你真的需要学习完整的 Python 教程或课程;这是非常简单的字符串操作;您不能一次学习一个 StackOverflow 问题的基础知识。

标签: python string split integer


【解决方案1】:

除非您需要更通用的方法,否则您可以根据您给出的示例中的空间进行拆分。


# set up your variable
my_var = "variable 1"

# You can split by the space in this instance, if all of your data will look the same
my_split = my_var.split(" ")

# prints variable
print(my_split[0])

# prints 1
print(my_split[1])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-11
    • 2013-05-23
    • 2020-05-18
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    • 2021-11-29
    相关资源
    最近更新 更多