字符串练习

  1. 去掉字符串中所有的空格
dict_a = "apple 1 wjh 234 CN 567 Banana 中文 /*&……%¥#%¥"
print(dict_a)
print(dict_a.replace(" ",""))

输出:

apple 1 wjh 234 CN 567 Banana 中文 /*&……%#%¥
apple1wjh234CN567Banana中文/*&……%#%¥
  1. 获取字符串中汉字的个数
dict_a = "apple 1 wjh 234 CN 567 Banana 中文 /*&……%¥#%¥"
print(dict_a)
count = 0
for s in dict_a:
    if '\u4e00' <= s <= '\u9fff':
        count += 1
print(count)

输出:

apple 1 wjh 234 CN 567 Banana 中文 /*&……%#%¥
2
  1. 将字母全部转换为大写和小写
dict_a = "apple 1 #%¥ wjh 234 *&… CN 567 Banana 中文 /…%¥"
print(dict_a)
#全部转换为大写
print(dict_a.upper())
#全部转换为小写
print(dict_a.lower())

输出:

apple 1 #%¥ wjh 234 *&… CN 567 Banana 中文 /…%¥
APPLE 1 #%¥ WJH 234 *&… CN 567 BANANA 中文 /…%¥
apple 1 #%¥ wjh 234 *&… cn 567 banana 中文 /…%¥ 
  1. 根据标点符号对字符串进行分行
dict_a = "apple 1 ,#%¥ wjh 234 \*&… CN 567 中文 \,…%¥ Banana"
dict_b = dict_a.split(',')
print(dict_a)
for i in dict_b:
    print(i)

输出:

apple 1 ,#%¥ wjh 234 \*&… CN 567 中文 \,…%¥ Banana
apple 1 
#%¥ wjh 234 \*&… CN 567 中文 \%¥ Banana
  1. 去掉字符串数组中每个字符串的空格(循环)
dict_a = ["  apple 1 #%¥ ""  \*&…  CN 中文"" -/+Banana"]
print(dict_a)
for c in dict_a:
    print(c.replace(" ",""))

输出:

['  apple 1 #%¥   \\*&…  CN 中文 -/+Banana']
apple1#%¥\*&…CN中文-/+Banana
  1. 随意输入心中想到的一个书名,然后输出它的字符串长度(len属性)
dict_a = str(input("请输入一个书名:"))
print("书名为:",dict_a)
print("字符串长度为:",len(dict_a))

输出:

请输入一个书名:假如给我三天光明
书名为: 假如给我三天光明
字符串长度为: 8
  1. 接收用户输入的字符串,将其中的字符进行排序,并以逆序输出,例如:acdefb - abcdeffedcba
dict_a = str(input("请输入字符串:"))
l = list(dict_a)
print(l)
l.sort()
dict_a = "".join(l)
print("排序后:",dict_a)
print("排序后再倒序输出:",dict_a[::-1])

输出:

请输入字符串:123456
['1', '2', '3', '4', '5', '6']
排序后: 123456
排序后再倒序输出: 654321
  1. 用户输入一句英文,将其中的单词以反序输出 例如:hello c sharp — sharp c hello
#用户输入一句英文,将其中的单词以反序输出 例如:hello c sharp --- sharp c hello
dict_a=input("请输入一句英文:").split(' ') 
dict_a.reverse()
dict_a=" ".join(dict_a)
print(dict_a)

输出:

请输入一句英文:hello world abc   
abc world hello
  1. 用户输入一句话,找出所有”呵“的位置
words = input("请输入一句话:")
index = 0
for i in words :
    if i =="呵":
        print(index)
    index += 1

输出:

请输入一句话:呵一二三呵五六七
0
4
  1. 有个字符串数组,存储了10个书名,书名有长有短,现在将他们统一处理,若长度大于10,则截取长度为8的子串,将统一处理后的结果输出
list1 = ["平凡的世界","假如给我三天光明","水浒传","西游记","三国演义","红楼梦","忘萱忧草之被遗忘的爱情","鲁滨逊漂流记","我是一个可大可小的人儿","稻草人"]
for i in range(0,len(list1)):
	a = list1[i]
	if len(a) > 10 :
		a = a[:8]
		list1[i] = a
print(list1)

输出:

['平凡的世界', '假如给我三天光明', '水浒传', '西游记', '三国演义', '红楼梦', '忘萱忧草之被遗忘', '鲁滨逊漂流记', '我是一个可大可小', '稻草人']  
  1. 用户输入一句话,找出所有”呵呵“的位置
words = input("请用户输入一句话:")
for i in range(0,len(words)):
	if words[i] == "呵" and words[i+1] == "呵":
		print("呵呵在%s的位置"%i)

输出:

请用户输入一句话:呵呵一二三呵呵一二三
呵呵在0的位置
呵呵在5的位置
  1. 如何判断一个字符串是否是另一个字符串的子串
word_one = input("请输入第一个字符串:")
word_two = input("请输入第二个字符串:")
if word_one in word_two:
	print("字符串一是字符串二的子串")
if word_two in word_one:
	print("字符串二是字符串一的子串")
else:
	print("不是相关字符串")

输出:

请输入第一个字符串:123456
请输入第二个字符串:23456
字符串二是字符串一的子串
  1. 如何验证一个字符串中的每一个字符均在另一个字符串中出现
word_one = input("请输入第一个字符串:")
word_two = input("请输入第二个字符串:")
flag = "true"
if len(word_one) < len(word_two):
	for i in word_one:
		index = 0
		for j in word_two:
			index += 1
			if i == j:
				break
			if index == len(word_two):
				flag = "false"
if len(word_two) <= len(word_one):
	for i in word_two:
		index = 0
		for j in word_one:
			index += 1
			if i == j:
				break
			if index == len(word_one):
				flag = "false"
if flag == "true":
	print("一个字符串中的每一个字符均在另一个字符串中出现")
else:
	print("一个字符串中的每一个字符没有都在另一个字符串中出现")

输出:

请输入第一个字符串:123456
请输入第二个字符串:23566
一个字符串中的每一个字符均在另一个字符串中出现
  1. 如何生成无数字的全字母的字符串
import random
import string
for i in range(0,random.randint(1,10)):
	print(random.choice(string.ascii_letters),end="")

输出:

xhzNLMbu
  1. 如何随机生成带数字和字母的字符串
import string
import random
for i in range(0,random.randint(1,5)):
	for k in range(0,random.randint(1,5)):

		print(random.choice(string.ascii_letters),end="")
	for j in range(0,random.randint(1,5)):
		print(random.randint(1,9),end="")

输出:

yl99YGDg325lqi4uAqW43922
  1. 如何判定一个字符串中既有数字又有字母
word = input("请输入一个字符串:")
if (word.isalnum() and not word.isdigit()) and not word.isalpha():
	print("既有数字和字母")
else:
	print("不是既有数字和字母")

输出:

请输入一个字符串:123456
不是既有数字和字母
PS C:\Users\CN\Desktop\云计算\python\作业\作业4> & D:/python/python.exe c:/Users/CN/Desktop/云计算/python/作业/作业4/cn16.py
请输入一个字符串:ac455
既有数字和字母
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值