Python笔记-字符串

print('hello world'.capitalize())   #让第一个单词首字母大写
print('hello world'.upper())        #全大写
print('hello world'.lower())        #全小写
print('hello world'.title())        #每个单词的首字母大写

#ljust,rjust    ljust(width,fillchar) 长度和填充的字符,默认是空格
print('hello'.ljust(10))
print('hello'.ljust(10,'-'))
print('hello'.rjust(10))
print('hello'.rjust(10,'-'))
#居中
print('hello'.center(20))
print('hello'.center(20,'*'))
#去空格
print('     hello       '.lstrip())
print('-----hello-----'.lstrip('-'))
print('     hello       '.rstrip())
print('     hello       '.strip())

names = 'zhangsan+lisi+wangwu+tom+jerry'
name = names.split('+')
print(name)
fruits = ['banana','apple','grape','pear','orange']
print('-'.join(fruits))
print('*'.join('hello'.title()))

word = 'hello'
x = input('请输入一个字符:')
for h in word:
    if x == h:
        print('您输入的内容存在')
        break
else:
    print('您输入的内容不存在')

name = 'Curry'
age = 32
print('大家好,我的名字叫',name,'今年',age,'岁了',sep='')
# print('大家好,我的名字叫 %s,今年%d岁了,'(name,age))

#字符串的format方法
#{}用来占位
x = '大家好,我的名字叫做{},今年{}岁了'.format('Curry',32)
print(x)

#{数字} 根据数字的顺序进行填入,从0开始
x = '大家好,我的名字叫做{1},今年{0}岁了'.format(32,'Curry')
print(x)

#{变量名}
x = '大家好,我的名字叫做{name},今年{age}岁了'.format(name = 'Curry',age = 32)
print(x)

#{数字}{变量}混合使用
x = '大家好,我的名字叫做{name},今年{0}岁了'.format(32,name = 'Curry')
print(x)

结果

Hello world
HELLO WORLD
hello world
Hello World
hello     
hello-----
     hello
-----hello
       hello        
*******hello********
hello       
hello-----
     hello
hello
['zhangsan', 'lisi', 'wangwu', 'tom', 'jerry']
banana-apple-grape-pear-orange
H*e*l*l*o
请输入一个字符:132
您输入的内容不存在
大家好,我的名字叫Curry今年32岁了
大家好,我的名字叫做Curry,今年32岁了
大家好,我的名字叫做Curry,今年32岁了
大家好,我的名字叫做Curry,今年32岁了
大家好,我的名字叫做Curry,今年32岁了

Process finished with exit code 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值