Python从0到1——②字符串(str)操作

这篇博客介绍了Python中的字符串操作,包括字符串的加法、乘法、长度获取、切分、合并、大小写转换、去空格以及子串查找。还讲解了使用split()、join()、upper()、lower()、strip()等方法,并展示了format()格式化输出的多种用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python从0到1——字符串(str)操作

其他常用操作见:https://blog.youkuaiyun.com/qq_33302004/article/details/112859327


print ('--字符串操作')
    # 字符串加法
print ('hello' + 'python')
    # 字符串乘法
print ('hello ' * 3)
    # 获取字符串长度
print (len('hello'))
    # 字符串切分
tang = '1 2 3 4 5'
print(tang.split())
tang = "1,2,3,4,5"
print(tang.split(","))    # 可以自定义分割字符
    # 字符串合并
tang2 = tang.split(",")
tang_str = " "
print(tang_str.join(tang2))
    # 大小写
tang = "Hello Python"
print(tang.upper())    # 全大写
print(tang.lower())    # 全小写
    # 去空格
tang = "        Hello Python            "
print(tang.strip())
print(tang.lstrip())
print(tang.rstrip())
    # 判断子串是否被包含在字符串中
tang = "Hello Python"
print('hello' in tang)
print('Hello' in tang)
print('llo' in tang)
    # format输出
print('{},{},{}'.format('hi','hello','yeah'))
print('{2},{1},{0}'.format('hi','hello','yeah'))    #根据参数的index确定位置
print('{para1},{para2},{para3}'.format(para1 = 'hi', para2 = 'hello',para3 = 'yeah'))    #直接把值赋给对应的参数名    
    # 判断子串是否被包含在字符串中
tang = "Hello Python"
print('hello' in tang)
print('Hello' in tang)
print('llo' in tang)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值