1.已知s2 = ‘123’,则:s2 * 3 = ___________; s2 + str(456) = ___________;
2.已知s1 = ‘123456789’。则:s1[:3] = ________; s1[-5:] = ____________; s1[::2] = _______;s1[::-3] = _______; int(s1[3:5]) + int(s1[-5:3:-1]) = ________。
3.已知s1 = “this is a test of Python”;统计字符串中s出现的次数:_____________;取出⼦串”test”的表达式为___________;获取s1的反序字符串的表达式为______________;将⼦串”test”替换为”exam”的表达式为:______________
4.已知字符串a = “aAsmr3idd4bgs7Dlsf9eAF”,要求如下:将a字符串的⼤写改为⼩写,⼩写改为⼤写的表达式为____________;转为⼩写字符串的表达式为_________________。
5.已知x=“abc”,y=“def”,z=[“d”,“e”,“f”],则x.join(y)= ________________;x.join(z) = __________________________。
6.把s1=”你好”,编码为字节流字符串:______________________
7.已知字符串:s2 = ‘ bj1902 ’,去除s2左边的空格的表达式为___________;去除s2右边的空格的表达式为____________;去除s2两边的空格的表达式为______________;
8.定义变量s=‘abcde’,写出下列代码运⾏的结果
s.upper() ________________
s[2::-1] ________________
s.isdigit() ________________
s * 2 ________________
‘*’.join(list(s)[:3]) ________________
答案:
1.‘123123123’; ‘123456’
2.‘123’; ‘56789’ ; ‘13579’; ‘963’; 50
3.s1.count(“s”); s1[10:14];
str1 = ""
for i in range(1, len(s1) + 1):
str1 += s1[-i]
s1.replace(“test”, “exam”)
a = "aAsmr3idd4bgs7Dlsf9eAF"
newStr = ""
for i in range(len(a)):
if a[i].upper() != a[i]:
newStr += a[i].upper()
else:
newStr += a[i].lower()
print(newStr)
a.lower()
5.“dabceabcf”; “dabceabcf”
6.s1 = b"你好"
7.s2.lstrip();s2.rstrip();s2.strip()
8.‘ABCDE’;‘cba’;False;‘abcdeabcde’;‘a*b*c’
1.输⼊⼀个字符串,⾃⼰统计该字符串有多少个字符,不允许⽤len()函数。
答案:
str1 = input("请输入一个字符串:")
newStr = str1
count = 0
while newStr != "":
newStr = newStr.replace(newStr[0:1], "", 1)
count += 1
# count += 1
print(count)
2.任给⼀个字符串,请验证是否是⼿机号,⼿机号为11位数字,开头三位必须是130,151,186。
-使⽤列表存储130,151,186;使⽤in判断。
-使⽤切⽚左取3位。
答案:
headNum = ['130', '151', '186']
num = input("请输入手机号码:")
if num[:3] in headNum and len(num) == 11:
print("验证成功")
else:
print("验证失败")
3.将字符串’1234567890’转换成‘ 1,234,567,890’输出,每3位⽤逗号隔开。
答案:
num = "1234567890"
newNum = "{:,}".format(int(num))
print(newNum)
4.输⼊两个字符串,从第⼀字符串中删除第⼆个字符串中所有的字符。
答案:
str1 = input("请输入一个字符串:")
str2 = input("请输入另一个字符串:")
for i in range(len(str2)):
str1 = str1.replace(str2[i], "")
print(str1)
5.任意输⼊⼀段⽂字,统计有多少个单词(⽤空格隔开)、多少个数字、多少字⺟、多少空格。
答案:
str1 = input("请输入一段文字:")
listStr = str1.split(" ")
numCount = 0
alaCount = 0
totalCount = 0
for i in range(len(str1)):
if str1[i].isdecimal() == True:
numCount += 1
if str1[i].isalpha() == True:
alaCount += 1
for i in range(len(listStr)):
totalCount += len(listStr[i])
spaceCount = len(str1) - totalCount
print("一共有{}个单词,{}个数字,{}个字母,{}个空格".format(len(listStr), numCount, alaCount, spaceCount))
1.input函数每次只能输⼊⼀个字符串,请实现如下输⼊格式:1,20,30。然后将获得的字符串分割,得到:三个整数:1 20 30,然后赋值给三个变量。
答案:
str1 = input("请输入三个整数,以逗号分隔:")
strList = str1.split(",")
x, y, z = int(strList[0]), int(strList[1]), int(strList[2])
print(x, y, z)
2.求字符串最后⼀个单词的⻓度,单词之间⽤空格分割。
答案:
str1 = input("请输入一段字符串:")
newList = str1.split(" ")
print(len(newList[len(newList) - 1]))
3.输⼊⼀个字符串,压缩字符串如下,aabbbccccd变成a2b3c4d1。
答案:
str1 = input("请输入一段字符串:")
strList = []
printStr = ""
for i in range(len(str1)):
if str1[i] in strList:
continue
strList.append(str1[i])
count = str1.count(str1[i])
printStr += str1[i] + str(count)
print(printStr)
4.给定⼀个句⼦(只包含字⺟和空格),将句⼦中的单词位置反转,单词⽤空格分割,单词之间只有⼀个空格,前后没有空格。例如:
“hello xiao mi”-> “mi xiao hello”
答案:
str1 = input("请输入一段字符串:")
strList = str1.split(" ")
newList = []
for i in range(1, len(strList) + 1):
newList.append(strList[-i])
print(" ".join(newList))
5.将s = “ajldjlajfdljfddd",去重并从⼩到⼤排序输出"adfjl"。
答案:
s = "ajldjlajfdljfddd"
strList = []
printStr = ""
for i in range(len(s)):
if s[i] in strList:
continue
strList.append(s[i])
strList.sort()
for i in range(len(strList)):
printStr += strList[i]
print(printStr)
6.使⽤密码表加密。
密码表加密是⼀种⼗分常⽤的密码加密⽅法,加密的原理是根据明⽂和密码表,加密形成密⽂,根据密⽂和密码表解密,读出明⽂。密码表可以是如下表所示:
normalStrList = ["a", "b", "c", "d", "e", "f", "g", "h",
"i", "j", "k", "l", "m", "n", "o", "p",
"q", "r", "s", "t", "u", "v","w", "x",
"y", "z", "1", "2", "3", "4", "5", "6",
"7", "8", "9", "0"]
encodeStrList = ["1", "8", "a", "c", "4", "y", "7", "b",
"x", "u", "i", "e", "p", "2", "3", "h",
"j", "s", "5", "o", "f", "w", "v", "0",
"z", "d", "l", "9", "g", "k", "m", "6",
"n", "q", "r" ,"t"]
str1 = input("请输入要加密的字符:")
encodeStr = ""
for i in range(len(str1)):
encodeNum = normalStrList.index(str1[i])
encodeStr += encodeStrList[encodeNum]
print(encodeStr)