c#网站开发网易云课堂百度云下载,网站建设和网站设计有什么区别,厦门建设局刘以汉,大连小型网站建设知识回顾
1、默认情况下#xff0c;input函数接收的数据是字符串类型。
2、字符串类型的关键词是str。
3、\n和\t都是转义字符#xff0c;\n用来换行#xff0c;\t用来留出一段固定长度的空白。
4、type函数能够用来查看变量的数据类型
5、数据类型的转换#xff0c;举…知识回顾
1、默认情况下input函数接收的数据是字符串类型。
2、字符串类型的关键词是str。
3、\n和\t都是转义字符\n用来换行\t用来留出一段固定长度的空白。
4、type函数能够用来查看变量的数据类型
5、数据类型的转换举个例子
x_str3.14
print(x_str)
y_intfloat(x_str)
print(y_int)
第一个是字符型、第二个是浮点类型。
6、有字符串 a good student如何取出其中的good字符串
sa good student
print(s[2:6]) 一、字符串的操作函数
通过索引可以取出字符串当中的某些内容除了取得字符串中的内容之外是否还能够对字符串做其它操作呢
如果某个字符串s1在另一个字符串s2中出现过那么我们称s1是s2的子串。
a good student-子串-good
1.字符串操作函数之len
Xiaotuzi 和 Maotouying 谁的名字的拼写长度比较长
要测量某个字符串的长度可以使用len函数
Xiaotuzi长度为8
Maotouying长度为10
Maotouying 名字的拼写长度比Xiaotuzi名字的拼写长度长。
2.len函数的使用方式len(参数)
如果参数是字符串 那么该函数会返回字符串的长度
练习
编程实现求出Xiaotuzi 和 Maotouying 两个名字的长度
name1Xiaotuzi
name2Maotouying
print(len(name1))
print(len(name2)) 3.字符串操作函数之find
I am a good student怎么判断这段话中是否含有good这个单词要查找个字符串是否是另一个字符串的子串可以使用find函数good 是 I am a good student的一个子串。
1.find函数的使用方式字符串.find(参数)
find函数可以查找字符串中是否含有某个子串 如果有则返回第一个子串的位置 否则返回-1此处的参数可以是要查找的子串。
编程实现判断good是否为I am a good student的子串 I a m a g o o d s t u d e n t 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
第一次出现子串的位置7
sI am a good student
tgood
print(s.find(t)) 2.I am a good student
怎么判断这段话中是否含有bad这个单词呢
sI am a good student
tbad
print(s.find(t)) 4.字符串操作函数之replace
I do not like Python
怎么把这段话中的do not替换成really这个单词
要将字符串中的子串替换成另一个字符串可以使用replace函数
I do not like Python
替换do not 为 really
I really like Python
替换后的字符串为I really like Python。
4.1、replace函数的使用方式 字符串.replace(参数1参数2)
replace函数可以将字符串中的子串替换成另一个字符串其中参数1是要被替换的子串参数2是新的要替换子串的字符串。 它的返回值是子串被替换后的新的字符串。
sI do not like Python
print(s.replace(do not,really)) 5.字符串操作函数之strip happy day
怎么把这段话中的前后空白部分给去掉
要去掉一个字符串的前后空白部分可以使用strip函数空白部分 happy day 空白部分去掉空白部分 替换后的字符串为happy day。
5.1、strip函数的使用方式字符串.strip( )
strip函数可以将字符串中的前后空白部分去掉此处没有参数。 它的返回值是字符串被去掉前后空白部分后的新的字符串
s happy day
print(s.strip())二、字符串的格式化输出
如何把变量按照一定的格式输出
比如 a 3 输出变量的值是3
这就需要用到 格式化符号来实现 格式化输出
1.什么是格式化
格式化按照一定的格式把字符串进行输出
在Python中可以在字符串中添加格式化符号使用运算符%来实现格式化输出常用的格式化符号有%d、%f 、%s
1.1字符串的格式化输出 - %d
%d是整数格式化符号可以将字符串中对应的部分替换成整型变量a的值
a3
print(变量的值是%d %a) 格式化符号 一定要和变量 配合使用吗
答不一定 可以不用和变量配合使用
print(I am %d years old%9) 有时我们需要特定的格式。
%03d、%3d、%-3d 可以让数字占3个空格。
%03d 如果不足3位左边用0补齐
%3d 如果不足3位左边用空格补齐
%-3d如果不足3位右边用空格补齐
print(%03d%6)
print(%3d%6)
print(%-3d%6) 2.字符串的格式化输出 - %f
%f是浮点数格式化符号可以将字符串中对应的部分替换成浮点数
print(pi%f%3.1415926) 如果需要特定格式 %.2f 可以保留小数点后2位
%6.2f 可以保留小数点后2位同时让结果占6个格左边用空格补齐
print(pi%.2f%3.1415926)
print(pi%6.2f%3.1415926) 刚刚介绍了保留小数点后若干位输出的一种方法实际上还有另外一种方法也可以用来控制小数点位数的输出。
3.字符串的格式化输出 - %s
%s是字符串的格式化符号可以将字符串中对应的部分替换为字符串
print(I am a %s%boy) 4.字符串的格式化输出
字符串中可以使用多个格式化符号
print(My name is %s,and I am %s years old%(Tom,10)) 练习
1.打印一份乘法表
print(%4d * %d %d%(1,1,1))
print(%4d * %d %d%(1,2,2),end)
print(%4d * %d %d%(2,2,4))
print(%4d * %d %d%(1,3,3),end)
print(%4d * %d %d%(2,3,6),end)
print(%4d * %d %d%(3,3,9)) 练习02
军队拦截了一条敌军发送的密码信息内容是
password C is the best computer programming language in the world.
请对这个字符串做以下处理
输出密码的长度判断这个字符串是否包含 ‘programming’ 这部分内容如果有输出它在字符串中第一次出现的位置如果没有这部分内容输出-1把C这部分内容替换为Python
passwordC is the best computer programming language in the world
print(len(password))
print(password.find(programming))
print(password.replace(C,Python)) 总结
字符串常用操作函数格式化占位符保留小数位数输出
所有代码
x_str3.14
print(x_str)
y_intfloat(x_str)
print(y_int)
sa good student
print(s[2:6])
name1Xiaotuzi
name2Maotouying
print(len(name1))
print(len(name2))
sI am a good student
tbad
print(s.find(t))
sI do not like Python
print(s.replace(do not,really))
s happy day
print(s)
print(s.strip())
a3
print(变量的值是%d %a)
print(I am %d years old%9)
print(%03d%6)
print(%3d%6)
print(%-3d%6)
print(pi%f%3.1415926)
print(pi%.2f%3.1415926)
print(pi%6.2f%3.1415926)
pi3.1415926
k4
print(%.*f %(k,pi))
print(I am a %s%boy)
print(My name is %s,and I am %s years old%(Tom,10))
print(%4d * %d %d%(1,1,1))
print(%4d * %d %d%(1,2,2),end)
print(%4d * %d %d%(2,2,4))
print(%4d * %d %d%(1,3,3),end)
print(%4d * %d %d%(2,3,6),end)
print(%4d * %d %d%(3,3,9))
passwordC is the best computer programming language in the world
print(len(password))
print(password.find(programming))
print(password.replace(C,Python))“学而不思则罔思而不学则殆。” 这句话强调了学习与思考相结合的重要性只有将学习和思考相互配合才能真正理解知识、掌握学问避免陷入迷茫或陷入空想。
感谢大家支持 文章转载自: http://www.morning.wyctq.cn.gov.cn.wyctq.cn http://www.morning.wtrjq.cn.gov.cn.wtrjq.cn http://www.morning.dqrhz.cn.gov.cn.dqrhz.cn http://www.morning.ummpdl.cn.gov.cn.ummpdl.cn http://www.morning.mfqmk.cn.gov.cn.mfqmk.cn http://www.morning.btqrz.cn.gov.cn.btqrz.cn http://www.morning.lbfgq.cn.gov.cn.lbfgq.cn http://www.morning.rnyhx.cn.gov.cn.rnyhx.cn http://www.morning.junyaod.com.gov.cn.junyaod.com http://www.morning.dlbpn.cn.gov.cn.dlbpn.cn http://www.morning.kzdwt.cn.gov.cn.kzdwt.cn http://www.morning.rmfh.cn.gov.cn.rmfh.cn http://www.morning.pctql.cn.gov.cn.pctql.cn http://www.morning.shxrn.cn.gov.cn.shxrn.cn http://www.morning.qszyd.cn.gov.cn.qszyd.cn http://www.morning.dangaw.com.gov.cn.dangaw.com http://www.morning.gmztd.cn.gov.cn.gmztd.cn http://www.morning.nwtmy.cn.gov.cn.nwtmy.cn http://www.morning.gcxfh.cn.gov.cn.gcxfh.cn http://www.morning.xllrf.cn.gov.cn.xllrf.cn http://www.morning.lfgql.cn.gov.cn.lfgql.cn http://www.morning.nytqy.cn.gov.cn.nytqy.cn http://www.morning.zypnt.cn.gov.cn.zypnt.cn http://www.morning.lrjtx.cn.gov.cn.lrjtx.cn http://www.morning.zsgbt.cn.gov.cn.zsgbt.cn http://www.morning.wxgd.cn.gov.cn.wxgd.cn http://www.morning.nbmyg.cn.gov.cn.nbmyg.cn http://www.morning.mbmh.cn.gov.cn.mbmh.cn http://www.morning.rzdpd.cn.gov.cn.rzdpd.cn http://www.morning.bkwd.cn.gov.cn.bkwd.cn http://www.morning.trhrk.cn.gov.cn.trhrk.cn http://www.morning.pznnt.cn.gov.cn.pznnt.cn http://www.morning.tckxl.cn.gov.cn.tckxl.cn http://www.morning.yjxfj.cn.gov.cn.yjxfj.cn http://www.morning.cznsq.cn.gov.cn.cznsq.cn http://www.morning.kmldm.cn.gov.cn.kmldm.cn http://www.morning.ltcnd.cn.gov.cn.ltcnd.cn http://www.morning.xjqhh.cn.gov.cn.xjqhh.cn http://www.morning.wgbmj.cn.gov.cn.wgbmj.cn http://www.morning.tsrg.cn.gov.cn.tsrg.cn http://www.morning.xfjwm.cn.gov.cn.xfjwm.cn http://www.morning.mtsgx.cn.gov.cn.mtsgx.cn http://www.morning.sfwcb.cn.gov.cn.sfwcb.cn http://www.morning.jjxnp.cn.gov.cn.jjxnp.cn http://www.morning.bpyps.cn.gov.cn.bpyps.cn http://www.morning.tqjwx.cn.gov.cn.tqjwx.cn http://www.morning.jmwrj.cn.gov.cn.jmwrj.cn http://www.morning.nmwgd.cn.gov.cn.nmwgd.cn http://www.morning.dcdhj.cn.gov.cn.dcdhj.cn http://www.morning.zwtp.cn.gov.cn.zwtp.cn http://www.morning.bnygf.cn.gov.cn.bnygf.cn http://www.morning.xnpml.cn.gov.cn.xnpml.cn http://www.morning.wklmj.cn.gov.cn.wklmj.cn http://www.morning.hdnd.cn.gov.cn.hdnd.cn http://www.morning.mqdr.cn.gov.cn.mqdr.cn http://www.morning.ccjhr.cn.gov.cn.ccjhr.cn http://www.morning.szzxqc.com.gov.cn.szzxqc.com http://www.morning.mqmmc.cn.gov.cn.mqmmc.cn http://www.morning.wfdlz.cn.gov.cn.wfdlz.cn http://www.morning.wbfly.cn.gov.cn.wbfly.cn http://www.morning.dbrpl.cn.gov.cn.dbrpl.cn http://www.morning.mynbc.cn.gov.cn.mynbc.cn http://www.morning.kmlmf.cn.gov.cn.kmlmf.cn http://www.morning.kpwdt.cn.gov.cn.kpwdt.cn http://www.morning.zpqbh.cn.gov.cn.zpqbh.cn http://www.morning.geledi.com.gov.cn.geledi.com http://www.morning.fwcjy.cn.gov.cn.fwcjy.cn http://www.morning.ycmpk.cn.gov.cn.ycmpk.cn http://www.morning.slfkt.cn.gov.cn.slfkt.cn http://www.morning.klltg.cn.gov.cn.klltg.cn http://www.morning.gmjkn.cn.gov.cn.gmjkn.cn http://www.morning.sxhdzyw.com.gov.cn.sxhdzyw.com http://www.morning.kwqwp.cn.gov.cn.kwqwp.cn http://www.morning.xzsqb.cn.gov.cn.xzsqb.cn http://www.morning.kmcby.cn.gov.cn.kmcby.cn http://www.morning.ksgjy.cn.gov.cn.ksgjy.cn http://www.morning.ssglh.cn.gov.cn.ssglh.cn http://www.morning.qztdz.cn.gov.cn.qztdz.cn http://www.morning.ybnps.cn.gov.cn.ybnps.cn http://www.morning.kxqwg.cn.gov.cn.kxqwg.cn