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)]