字符串学习
---------- input交互输入----------
name = input( 'please enter your name:' )print (name)
---------- input 交互输入计算----------
value1 = 1024 * 512value2 = input('num1:')value3 = input('num2:')print (value1)print (value2)print (value3)print (value1 * value2)print (value1 * value3)#当value2 * value3的时候不能如上表示
---------- in 判断变量/值中包含的字符串----------
s = "Wind and cloud"weather1 = "Wind" in sweather2 = "SB" in sprint (weather1)print (weather2)
---------- 变量元组 ----------
Wind = "weather1"Paul = "name"Rain = "weather2"_Coll_1 = ['Wind','Paul','Rain']print(_Coll_1)_Cont_1 = "Wind" in _Coll_1print (_Cont_1)
---------- type 查看对象的类,一般字符串都是标准输入----------
temp1 = "123"_123_io = type(temp1)print(_123_io)temp2 = "hey"_hey_io = type(temp2)print(_hey_io)
---------- dir 查看功能----------
temp = "wind"print(dir(temp))
---------- upper 小写转换成大写----------
temp = "wind"print(temp.upper())
---------- count 字符串统计一定要在一对引号之内的字符串----------
_name_coll = ("paul,vencent,sunny,anny,should,bilibili,david,paul")_coll = _name_coll.count("nn")print(_coll)
---------- endswith 字符串尾部输出定义----------
temp = "hello"print(temp.endswith('e',0,2)) #字符串的0-2的字符he,是以e结尾,python中如果从字符串开头查找字符,必须从0位开始
---------- center 占位输出----------
a1 = "alex"a2 = a1.center(20,'*')print(a2)#字符总数20个,以alex为中心,两边以*号填充
---------- count 统计指定字符串数量----------
a1 = "alex is alpa"a2 = a1.count('al',5,12)print(a2)
---------- expandtabs 分隔符设计20位----------
content = "hello\t123"print(content)print(content.expandtabs())print(content.expandtabs(20))
---------- find 字符串位数查找
s = "alex hello"print(s.find("h"))print(s.find("p"))#找不到的字符,会显示负数
---------- str 字符串标准输出,字符串位置输出定义
str = 'Runoob'print (str[0:3])print (str[0])print (str[2:5])print (str[2:])print (str * 2)print (str + "TEST")
---------- \n 本身是换行符,r将字符串中特殊字符转化为普通字符
print('Ru\nnoob')print(r'Ru\nnoob')
---------- format 占位替换,有序排列
x = "hello {0}, age {1}"print(x)y = x.format("paul","27")print(y)
---------- join
alex = "name1"should = "name2"paul = "name3"dio = "name5"name = [alex ,should ,paul ,dio]_name_join = "".join(name)print(_name_join)#将值变成一个整体字符串输出,如果不用元组,只是字符串,可以使用双引号把字符串引起来,加入空格,是字符串相互隔开。name = "alex ","should ","paul ","dio "print (name)_name_join = "".join(name)print(_name_join)
---------- replace 替换字符串
x = "alex is alex"y = x.replace("al","AA")print(y)
---------- split 对字符串进行切片分隔
x = "alex\nalex"y = x.split()print(y)
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦