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)))
相关推荐
做运维的阿瑞6 小时前
Python 标准库汇总:分类速览与常用模块清单
linux·运维·python
why-geo6 小时前
Hermes Agent 与 Python 的会产生什么样的碰撞
人工智能·python·ai编程
正经教主6 小时前
【FDE系列】阶段2:Day 29:FastAPI 进阶 — Pydantic 模型与完整 CRUD 实战
人工智能·python·fde
gCode Teacher 格码致知7 小时前
Pandas教学-6:coerce详解
python·pandas
梦在远山后7 小时前
Electron 与 FastAPI 如何完成流式 Agent 对话
python·langchain·agent
大衛說7 小时前
12 · 文件 I/O 与序列化
python
小静AI工程实验室7 小时前
Python 爬虫中文乱码排查:严格解码、UTF-8 BOM 与 JSON 转义的 12 项实验
字符编码·爬虫·python
天天被压力7 小时前
【跨市场数据实战 #08】可转债折价机会怎么筛:3个接口抓比价、列表和实时盘口
java·人工智能·python
weixin199701080167 小时前
[特殊字符]️《从0到1搭多平台二手ERP中台:闲鱼+淘宝+京东+拼多多+Mercari统一调度》(附Python源码)
python
王国强20097 小时前
uv 深入指南:重新理解 Python 的包管理、依赖解析与项目工程化
python