题解 | #字符串分隔#

字符串分隔

https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7

def split_string(s):  
    # 如果字符串长度不是8的倍数,那么在其末尾添加0,使其长度达到8的倍数  
    if len(s) % 8 != 0:  
        s += '0' * (8 - len(s) % 8)  
  
    # 使用列表推导式来拆分字符串,每8个字符为一个片段  
        processed_list = [s[i:i+8] for i in range(0, len(s), 8)]  
        result1 = '\n'.join(processed_list) 
        return result1 
    else:
        processed_list2 = [s[i:i+8] for i in range(0, len(s), 8)]  
        result2 = '\n'.join(processed_list2) 
        return result2 
        # return s
  
# 接收用户输入  
input_strings = input().split()  
  
# 对每个输入的字符串进行处理并输出结果  
for s in input_strings:  
    print(split_string(s))

#我的实习求职记录#
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务