python --创建固定字符串长度,先进先出

python 复制代码
a = '123'

def concatenate_within_limit(b, new_string):
    # 计算新字符串与a的长度之和
    a = b
    total_length = len(a) + len(new_string)

    # 如果长度超过1024,从前面删除足够的字符
    if total_length > 5:
        diff = total_length - 5
        a = a[diff:] + new_string  # 删除前diff个字符,并拼接新字符串
    else:
        a += new_string  # 长度未超过1024,直接拼接新字符串

    # 返回处理后的a
    return a

for i in range(4,10):
    print(concatenate_within_limit(a, str(i)))
相关推荐
kiki-bf21 小时前
使用python把图片转为word
开发语言·python·word
长安牧笛21 小时前
适老版线上超市,针对老年人的小程序,图片大,字大,主打日常用品+送货上门,还能电话下单,解决老年人不会用普通电商的问题。
python
@#---21 小时前
如何准确判断json文件并且拿到我想要的信息
android·python·json
光羽隹衡21 小时前
Python中的网络爬虫
开发语言·爬虫·python
Vantastic99921 小时前
基于Qwen Agent的多智能体协作系统:实现AI团队协同工作流
人工智能·python
不会写DN21 小时前
fmt 包中的所有 Print 系列函数
开发语言·后端·golang·go
zhongtianhulian21 小时前
陶瓷行业导航网站:景德镇信息大全 — 采购指南与政策解读
人工智能·python
南棱笑笑生21 小时前
20251213给飞凌OK3588-C开发板适配Rockchip原厂的Buildroot【linux-6.1】系统时适配CTP触摸屏FT5X06
linux·c语言·开发语言·rockchip
电子_咸鱼21 小时前
常见面试题——滑动窗口算法
c++·后端·python·算法·leetcode·哈希算法·推荐算法
深圳佛手1 天前
jupyter notebook如何使用虚拟环境?
人工智能·python