python-字符串

#coding=utf-8
str1="字符串类型"
str2=r"有特殊字\n%&&^^**^^"

print(str1, str2)
print(str1+str2)
print('字符串str1长度%d,字符串str2长度%d',len(str1),len(str2))
print('start空,end空'+str1[:]+'\n指定start,end,step'+str2[0:4:2]+'\n指定start,end:'+str2[1:3]+'\n索引'+str2[0]+'\n指定end'+str2[:3]+'\n指定start'+str2[1:])
str3="李白,张三,李四,天明,天空,天天,白天"
str4='李白 张三 李四 天明 天空  天天 白天  暖和 明天 昌明'
print("分割字符串:按,分割",str3.split(','))
print("分割字符串:按默认空格分割",str4.split())
print("分割字符串:指定分割次数3",str3.split(',',3))

print("合并字符串"+str3.join(str4.split(',')))
print("合并字符串"+'@'.join(str4.split()))

str5='''total of 10.26 billion stems of fresh cut flowers were sold in 2021 at the Kunming Dounan Flower Market in Southwest China's Yunnan province, with business volume hitting 11.24 billion yuan ($1.8 billion)."
"Dounan market sells fresh cut flowers, bonsai and green seedlings as its core products, and also operates a wide range of services, such as gardening, packaging and logistics, according to the market's management committee."
"Trade can be conducted both onsite and via electronic auction services, and consumers can buy flowers through e-commerce platforms.'''

print("str5:"+str5)
print("某字符串出现的次数"+str(str5.count("can")))
print("某字符串出现的次数"+str(str5.count("can",10)))
print("某字符串出现的次数"+str(str5.count("can",10,200)))

print("查找某字符串是否包含指定的字符",str5.find('can'))
print("查找某字符串是否包含指定的字符串",str5.find('can',20,100))
print("查找某字符串是否包含指定的字符串",str5.find('can',200))
print("查找某字符串是否包含指定的字符串",str5.index('can'))
try:
    print("查找某字符串是否包含指定的字符串", str5.index('can', 20, 100))
    print("查找某字符串是否包含指定的字符串",str5.index('can',20))
    print("查找某字符串是以指定的字符串开头", str5.startswith('total', 20, 100))
    print("查找某字符串是以指定的字符串开头", str5.startswith('total', 20))
    print("查找某字符串是以指定的字符串开头", str5.startswith('total'))

    print("查找某字符串是以指定的字符串结尾", str5.endswith('total', 20, 100))
    print("查找某字符串是以指定的字符串结尾", str5.endwith('total', 100))
    print("查找某字符串是以指定的字符串结尾", str5.endswith('total'))
except ValueError as e:
    print("错误:",e)


print("字符串全小写"+str5.lower())
print("字符串全大写"+str5.upper())

str6="  苦硒鼓顶替要林  需要 顶替顶替顶替枯顶替顶替顶替无可奈何  "
print("去掉左右空格",str6.strip())
print("去掉左边空格",str6.lstrip())
print("去年右边空格",str6.rstrip())

str7="%%%%%压顶地顶替阿斯蒂芬胜多负少夺顶替顶替顶替顶替顶替顶替村夺梦想成真 sdf%%%%%%%%%%"
print("按指定字符去掉左右",str7.strip('%'))
print("按指定字符去掉左边",str7.lstrip('%'))
print("按指定字符去掉右边",str7.rstrip('%'))

print("重复显示",str7*2)
print("遍历1:")
for every in str7:
    print (every,end=' ')
print("遍历2:")
for i in range(len(str7)):
    print(str7[i],end=' ')
print("遍历3:")
for i ,every_char in enumerate(str7):
    print(str(i)+every_char,end=' ')

print()
str8="AbcdEFGHIjklmnOPQRStUvWxYz"
print("翻转大小写:",str8.swapcase())

print("My name is %s and weight is %d kg!" % ('Zara', 21) )
temp="编号:%d,公司名称:%s,官网:http//www.%s.com"
print(temp%(1,'百度','baidu'))
temp2="编号:{:s},公司名称:{:s},官网:http//www.{:s}.com"
print(temp2.format('2','好123','hao123'))

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值