目录
4.len('您好,Helen')和len("\n\t\r")的值都是多少?
5."china" in "I love china"的值是true还是false?
6."I love china".find("china")的值是多少?
3.从键盘获取交互式输入的一个人的18位的身份证号,以类似于“2001年09月12日的形式输出此人的出生日期。
2.简单题
1.字符串有哪几种表示形式:
' '," ",''' '''
,其中单引号和双引号为单行字符串,而三引号为多行字符串。''' '''也表示多行注释,开始要有,结尾也要有。 //单引号,双引号都可以进行多行注释
2.format()
方法的参数有哪些?
{}为占位符,占位符可以用来接收format()方法中的参数
1.位置匹配参数
"{} is {} years old".format ("Rose",18) 'Rose is 18 years old' "{0}is {1} years old".format ("Rose",18) 'Rose is 18 years old' "Hi,{0}!{0}is{1}years old".format("Rose",18) 'Hi,Rose!Rose is 18 years old'
2.使用键值对的关键字参数匹配
"{name}was born in {year},He is {age} years old".format (name="Rose",age= 18,year=2000) 'Rose was born in 2000,He is 18 years old'
3.索引参数
student=["Rose",18] school=("Dalian","LNNU") "{1[0]} was born in 0[0]),She is {1[1]}years old".format (school,student) 'Rose was born in Dalian,She is 18 years old'这个解释一下:你可以将上面的school和student看成两行,school为第0行,student为第1行。
1 0 1
0 0 1
这样的话会更好理解。
3.字符串合并与拆分的方式
**split(sep,num)**
:sep为分隔符分隔字符串,如果num有指定值,则仅截取num个子字符串
**jion(sep)**
:以指定字符串为分隔符,将sep中所有的元素合并为一个新的字符串>>> lst=['hi','python!','hi','java!'] >>> s="" >>> s.join(lst) 'hipython!hijava!' >>> lst=['How','are','you'] >>> s=" " #s就是这些字符串之间以啥形式隔开 >>> s.join(lst) 'How are you'>>> str="how are you" >>> str.split(" ") #以空格作为分隔符 ['how', 'are', 'you']
4.len('您好,Helen')
和len("\n\t\r")
的值都是多少?
len(str)
这个时返回字节长度的
len('您好,Helen')
返回的值是:8
len("\n\t\r")
返回值是3 因为\n,\t,\r,才算一个字符,即转义字符
5."china" in "I love china"的值是true还是false?
**ture**
这个是用来检查字符串中是否包含指定的子字符串,如果包含则返回true,不包含返回false。
china是子字符串,就是检查I love china中是否包含china,包含则返回true。
6."I love china".find("china")的值是多少?
find()是用来子字符串在原始字符串中的位置,如果存在返回字符串首次出现的索引,如果不存在则返回-1.题目中的值是7.
3.编程题
2.给出一个字符串,将其中的字符“E”用空格替换后输出。
a=input("please enter:") b=a.replace("E"," ") print(b) #输出结果: please enter:Please Enter Please nter
有些草率
3.从键盘获取交互式输入的一个人的18位的身份证号,以类似于“2001年09月12日的形式输出此人的出生日期。
id_number=input("please enter:") year=id_number[6:10] month=id_number[10:12] day=id_number[12:14] birth=year+"年"+month+"月"+day+"日" print("出生日期:{}".format(birth))
因为剩下的题我在作业本上做了,我就没在电脑上写
不要将format中的format(birth)写成format("birth")因为第二种是输出字符串