pythonday02

字符串

1.单引、双引号都可以定义字符串
name=
'tom';
name1=
"tomcat";
print(name,name1);

'''可以多行写代码不用把代码写在一行里
\t代表空格
\n代表换行'''


html=
'''<html>
<hand>
\t</hand>
<body>
\n
</body>
</html>'''
;
print(html)

 

 

 

2.截取字符串

str="zhangsan@qq.com";

切片 []  [:]  [ : : ]

获取单个字符 下标

print("第一字符:"+str[0])

print(len(str)) 查看字符长度

print("最后一个字符:"+str[14])

print("最后一个字符:"+str[len(str)-1])

print("最后一个字符:"+str[-1])

mystr[1:5]截取14下标的字符串

print(str[:5]) #从零 开始到结束

print(str[:]);#所有

print(str[5:]); #从几开始到结束



print(str[-5:]); #-5到结尾

print(str[-5:-2]); #-5到结尾 不包含后一位

其中最后一个参数代表步长

print(str[::])

从后往前 -1-1-1-1

print(str[-1:-5:-1]);

print( str[-1::-1])  #倒叙

print(str[::-1])  #倒叙

str() 函数 将非 字符型转换成字符型
email ="hang@bdqn.cn";

print("an in email:"+ str( 'an' in email ));

print("at  not in email:"+ str( 'at'  not in email ));

 3.r/R:原样输出字符串, 取消特殊性

print("hello\tpython");

print(r"\t 代表一个制表符 \n 换行符");
4.字符串格式化

第一种 %s字符  %d整数  %f 浮点型 %.2f 保留2 小数 四舍五入

print("name:%s"%'tom');

print("name:%s age:%d money:%.2f"%('tom',33,90.456));

第二种   .format()格式化:



print('name:{0} age:{1}'.format("tom",33));

print("name:{name} age {age}".format(name="tom",age=33));

print("name={{{0}}}".format("tom"))



5.字符串的一些函数:

word="""hello python world""";

字符串的长度

print("字符串的长度:{0}".format(len(word)));

count(str,start= 0,end=len(string))  str 出现的次数

print("字符串中 o 的次数:{0}".format(word.count("on",0,7) ));

#capitalize()将字符串的第一个字符转换为大写

print(word.capitalize());

# title():将每一个单词首字母大写 标题化

print(word.title());

find 返回找到的字符串的下标 找不到=-1

print("h in word of index:{0}".format( word.find('q')));

 rfind 从右面开始找 beg-end 指定一个范围

print("h in word of index:{0}".format( word.rfind('h',-13,-1)));

index():没有则抛出异常。

print("h in word of index:{0}".format( word.index('h')));

find() vs index():如果找不到,index()产生异常,find()则返回-1

replace():mystr.replace("ll","LL"),但是原字符串不会被改变



print(word.replace("hello","nihao:"));

word=word.replace("hello","nihao:");

print(word)



split(str="", num=string.count(str)):mystr.split(" "):

如果split()什么都不写,就是将通过\t和空格进行拆分 num=分割几次

s=word.split("o",5);

print(s)





我电话

phone="15510001040@163.com";

print(phone.startswith("155"))

print(phone.endswith("1040"))

print(phone.upper().lower())

name="  liSi";

字符串输出时怎样对齐,括号里写大小 达到一个新的高度 以什么填充

name=name.ljust(10," ");

print(name);

# strip(),lstrip(),rstrip()去除空格

print(name.strip());

print(name.lstrip());

print(name.rstrip());





partition():mystr.partition("xxx")以此字符串为节点拆分

 返回头、分割符、尾三部分 返回头、尾两个空元素的3个元组。



name="hello-python-world";

print(name.split("-"))

print(name.partition(" "))

小练习:

email="zhangsan@163.com";

a=email.find("@");

if a==-1:

    print("邮箱错误 没有包含 @ ");

elif a == 0:

    print("邮箱错误 @不能出现在第一位 ");

elif(email.find(".")==-1):

    print("邮箱错误 没有包含 . ");

elif(email.find(".")<email.find("@")):

    print("邮箱错误  . 的位置应该在@ 后面。。 ");

else:

    print("邮箱合法 用户名是{0}".format( email[:a] ));

 

 

 

s=input('请输入你的邮箱:')

q=s.find('@');

w=s.find('.');

if(q<w):

    print('合法');

    print('您的用户名是:{0}'.format(s[:q]));

else:

    print('不合法');
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值