Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果你有一个字符串
string='abcdefg'
并且您想检查字符串的长度是否可以被 3 整除
len(string)
你会使用什么命令?
您可以使用模(除法余数)运算符%:
%
if len(s) % 3 == 0: ...
如果要将字符串剥离为可被 3 整除的长度,请使用
s[:len(s) // 3 * 3]
或者
s[:-(len(s) % 3)]