【发布时间】:2022-01-04 19:41:31
【问题描述】:
char = "RV49CJ0AUTS172Y"
要获得此输出:
separated = "RV49C-J0AUT-S172Y"
我试过了:
split_strings = []
n = 5
for index in range(0, len(name), n):
split_strings.append(name[index : index + n])
但这并没有如我所愿 This shows a good method but I want them on the same line with dashes between them
【问题讨论】:
-
欢迎来到 StackOverflow,可以扩展拆分背后的逻辑吗?如果字符串的长度不能被 3 整除会怎样?
-
您可以简单地使用:
s[0:5] + "-" + s[5:10] + "-" + s[10:](将char重命名为s,因为char不是一个好的字符串名称)只要字符串是10-15 个字符长度。