python 学习day9(字符串之(大小写转换,删除空白字符,左中右对齐,判断))

字符串大小写转换

【1】capitalize():将字符串第一个字符转换为大写

mystr='hello word and itcast and itheima and python'

print(mystr.capitalize())
#  Hello word and itcast and itheima and python

【2】title():将字符串每个单词首字母转换为大写

mystr='hello word and itcast and itheima and python'

print(mystr.title())
#  Hello Word And Itcast And Itheima And Python

【3】lower():在字符串中大写变小写

mystr='Hello Word And Itcast And Itheima And Python'

print(mystr.lower())
#   hello word and itcast and itheima and python

【4】upper ():将字符串小写变大写

mystr='hello word and itcast and itheima and python'

print(mystr.upper())
#  HELLO WORD AND ITCAST AND ITHEIMA AND PYTHON

删除

【1】:lstrip():删除字符串左侧空白字符

mystr='    hello word and itcast and itheima and python'

print(mystr.lstrip())
#  输出:hello word and itcast and itheima and python

【2】rstrip():删除最右边的空格

mystr='    hello word and itcast and itheima and python    '

print(mystr.rstrip())
#  输出:    hello word and itcast and itheima and python**

【3】strip():删除两侧的空格

mystr='    hello word and itcast and itheima and python    '

print(mystr.strip())
# 输出:hello word and itcast and itheima and python

左中右对齐

【1】ljust():返回一个原字符串左对齐,并使用指定字符(默认空格)填充至对应长度的新字符串
1.语法:

字符串序列.ljust(长度,填充字符)

【2】rjust():右对齐
【3】center():居中对齐,与just一致
实例:
在这里插入图片描述

判断真假

返回布尔型的数据类型:True和False
【1】stratswith():检查字符串是否是以指定子串开头,是则返回True,否则返回False。如果设置开始和结束位置下标,则在指定位置内检查
1.语法:

字符串序列.stratswith(子串,开始下标,结尾下标)

体验:

mystr='hello word and itcast and itheima and python    '

print(mystr.startswith('hello'))
#  True
print(mystr.startswith('hellos'))
#  False

【2】endswith():看结尾是不是某个字符串
用法类似stratswith

mystr='hello word and itcast and itheima and python'

print(mystr.endswith('python'))
#  True
print(mystr.endswith('hellos'))
#  False

【3】isalpha():如果字符串至少一个字符并且所有字符都是字母则返回True,否则False

mystr='hello word and itcast and itheima and python'

print(mystr.isalpha())
#  False
mystr='hellowordanditcastanditheimaandpython'
print(mystr.isalpha())
#  True

【4】isdigit():如果字符串只包括数字则True否则False

mystr='hellowordanditcastanditheimaandpython  '
print(mystr.isdigit())
#  False
mystr='11122222'
print(mystr.isdigit())
#  True

【5】isalnum():如果字符串至少有一个字符并且所有字符都是数字或者字母则True否则False

mystr='hellowordanditcastanditheimaandpython  '
print(mystr.isalnum())
#  False
mystr='hello1word2and3itcast4and5itheima6and7python'
print(mystr.isalnum())
#  True

【6】isspace():如果字符串中只包括空白,则True否则False

mystr='hellowordanditcastanditheimaandpython  '
print(mystr.isspace())
#  False
mystr1='   '
print(mystr1.isspace())
#  True

字符串学完了,我脑袋也忘记完了

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值