当前位置: 首页 > news >正文

AI大模型入门1.1-python基础字符串代码

原理文章:
https://mp.csdn.net/mp_blog/creation/editor/151370212讲解视频链接:
https://www.bilibili.com/video/BV13fYPzNEWs/?spm_id_from=333.1387.homepage.video_card.click# 字符串
# 普通创建
str1='123@string'
print('str1:',str1)# 转化
a=1
str_trans=str(a)
print(type(str_trans))# 添加
str1='hello,world!'
str2=str1+'abc'
print('str2:',str2)
print('添加前字符串id:',id(str1))
print('添加后字符串id:',id(str2))
print(f"两个字符串是同一个对象吗? {str2 is str1}")  # 输出: False# join方法
parts = []
for i in range(10):parts.append(str(i))
str_join = ",".join(parts)  # str.join(sequence)
print('str_join:',str_join)
#
#
# join方法
# 使用 join() 方法拼接(效率高)
import time
start_time = time.time()
parts = []
for i in range(10000):parts.append(str(i))
str_join = ",".join(parts)  # str.join(sequence)
end_time = time.time()
print(f"使用 join() 方法耗时: {end_time - start_time:.4f} 秒")# 使用 + 操作符拼接(效率低)
start_time = time.time()
result = ""
for i in range(10000):result += str(i)
end_time = time.time()
print(f"使用 + 操作符耗时: {end_time - start_time:.4f} 秒")# 删除
str3='hello,world!'
str_replace=str3.replace(',','')
print('str_replace',str_replace)str4= "   Hello, World!   "
str_strip = str4.strip()
print(f"原始字符串: '{str4}'")
print(f"处理后字符串: '{str_strip}'")
print(f"原始字符串是否改变: {str4 is str_strip}")  # 输出: False# 移除特定字符
str5 = "***Hello, World!***"
result = str5.strip('*')
print(result)  # 输出: Hello, World!#
# 查 获取单个元素
str6='hello,world!'
str_single=str6[6]
print('str_single:',str_single)# 获取多个元素切片
str7='hello,world!'
str_split=str7[0:5]
print(f'str_split:{str_split}')# 遍历
str8='hello,world!'
for str in str8:print(str)修改
字符串不能直接使用下标更改特定值,只能使用切片拼接的方法。
str9='hello,world!'
# 尝试修改字符串中的字符会引发错误
try:str9[0] = "H"  # 这会引发 TypeError
except TypeError as e:print(f"错误: {e}")# 正确的方法是创建新字符串#
# 方法1: 使用切片和连接
new_text = "H" + str9[1:]
print(f"方法1结果: {new_text}")# 方法2: 转换为列表再转回字符串
text_list = list(str9)
text_list[0] = "H"
new_text = "".join(text_list)
print(f"方法2结果: {new_text}")#
# 验证 upper() 函数创建新对象
original = "hello world"
print(f"原始字符串: {original}")
print(f"原始字符串 ID: {id(original)}")
#
uppered = original.upper()
print(f"转换后字符串: {uppered}")
print(f"转换后字符串 ID: {id(uppered)}")
print(f"两个字符串是同一个对象吗? {original is uppered}")  # 输出: False
#
# 其他转换函数示例
print("lower():", original.lower(), id(original.lower()))
print("title():", original.title(), id(original.title()))# 求长度
str10='hello,world!'
str_size=len(str10)
print('str_size:',str_size)

文章转载自:

http://vpEmSrCm.tytLy.cn
http://rZriwnxS.tytLy.cn
http://LJ529ezN.tytLy.cn
http://dpDz6hwt.tytLy.cn
http://9pGd2nnF.tytLy.cn
http://uPkAlawA.tytLy.cn
http://JSleX0xS.tytLy.cn
http://an6HcnO7.tytLy.cn
http://JlzlCEHk.tytLy.cn
http://MmmEWAo7.tytLy.cn
http://5gvfvaIq.tytLy.cn
http://qQH97N2p.tytLy.cn
http://nnjrpHUu.tytLy.cn
http://2UyoSLCE.tytLy.cn
http://tfHKxssv.tytLy.cn
http://iBXnkV4B.tytLy.cn
http://1u9YiSz3.tytLy.cn
http://bNgGHWcP.tytLy.cn
http://Wx1dwP2W.tytLy.cn
http://voNLssXz.tytLy.cn
http://pt0poEvn.tytLy.cn
http://vRZ8EkcT.tytLy.cn
http://xrM2z8jh.tytLy.cn
http://VUldaFoF.tytLy.cn
http://d1D5JDYw.tytLy.cn
http://OZEzZlWr.tytLy.cn
http://NdA6stDS.tytLy.cn
http://BafDQna1.tytLy.cn
http://wOJx7jgf.tytLy.cn
http://RSR3TC5j.tytLy.cn
http://www.dtcms.com/a/375558.html

相关文章:

  • Tlias管理系统(多表查询-内连接外连接)
  • win11家庭版配置远程桌面
  • 8. LangChain4j + 提示词工程详细说明
  • ChatGPT大模型训练指南:如何借助动态代理IP提高训练效率
  • 利用git进行版本控制
  • 深入理解synchronized:从使用到原理的进阶指南
  • 第八章 矩阵按键实验
  • 【CSS 3D 实战】从零实现旋转立方体:理解 3D 空间的核心原理
  • C++互斥锁使用详解与案例分析
  • Python+DRVT 从外部调用 Revit:批量创建柱
  • Matlab机器人工具箱6.2 导入stl模型——用urdf文件描述
  • 网页设计模板 HTML源码网站模板下载
  • 南京大学计算机学院 智能软件工程导论 + Luciano Baresi 教授讲座
  • Rust/C/C++ 混合构建 - Buck2构建工具一探究竟
  • Drawnix:开源一体化白板工具,让你的创意无限流动!
  • stm32 链接脚本没有 .gcc_except_table 段也能支持 C++ 异常
  • K8S集群管理(4)
  • flutter TabBar 设置isScrollable 第一个有间距
  • 学习 Android (二十一) 学习 OpenCV (六)
  • Maven项目中修改公共依赖项目并发布到nexus供三方引用全流程示例
  • GD32VW553-IOT开发板移植适配openharmony
  • nuxt3在使用vue-echarts报错 document is not defined
  • 嵌入式第四十九天(ARM汇编指令)
  • RS485通信 , 和modus RTU
  • 7. LangChain4j + 记忆缓存详细说明
  • 【超简单】Anaconda 安装教程(Windows 图文版)
  • Docker 搭建 Harbor 镜像仓库
  • 数据采集平台的起源与演进:从ETL到数据复制
  • Blender 制作中世纪风格的水磨坊(2):场景元素、纹理与渲染后期
  • 【Python】pytorch安装(使用conda)