【问题标题】:How to pass several separators in method ( .split(....) ) in Python [For two and more variables in one string]如何在 Python 中的方法( .split(....) )中传递多个分隔符 [对于一个字符串中的两个或多个变量]
【发布时间】:2021-12-04 18:12:54
【问题描述】:

# Task - input: amount of students for three classes ; output: amount of required desks

import math

First_Class, Second_Class, Third_Class = input("Enter amount of students for each class : ").split(sep=",")
Amount_Of_Desks = math.ceil(int(First_Class) / 2) + math.ceil(int(Second_Class) / 2) + math.ceil(int(Third_Class) / 2)
print(f" Sum amount of required desks is : {Amount_Of_Desks}")

我只想在一个字符串中有两个分隔符

【问题讨论】:

标签: python string methods split delimiter


【解决方案1】:

您可以在调用 split 函数之前使用 replace 函数将分隔符转换为一个分隔符

import math

First_Class, Second_Class, Third_Class = input("Enter amount of students for each class : ").replace(';',',').split(sep=",")
Amount_Of_Desks = math.ceil(int(First_Class) / 2) + math.ceil(int(Second_Class) / 2) + math.ceil(int(Third_Class) / 2)
print(f" Sum amount of required desks is : {Amount_Of_Desks}")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2013-05-22
    • 2023-04-10
    • 2012-06-18
    相关资源
    最近更新 更多