case2.1:统计子串在字符串中出现的次数:
# -*- coding:utf-8 -*-
# Copyright (c)2019, KanShan,All rightsreserved
# Author:KanShan
#Description:用户输入一个字符串和一个子串,例如‘ABCDCDC’和'CDC',输出子串出现的次数。
mother_chars = input("please input a long characters:")
son_chars = input("please input a short characters:")
count = 0
for i in range(len(mother_chars) -1):
if mother_chars[i: i+len(son_chars)] == son_chars:
count += 1
print("The counts of %s is in %s is:%d" % (son_chars, mother_chars, count))
image.png
case2.2:修改字符串位置对应的字符
# -*- coding:utf-8 -*-
# Copyright (c)2019, KanShan,All rightsreserved
# Author:KanShan
#Description:用户输入一个字符串,修改该字符串中哪个位置的字符,程序就会输出修改后的结果
strings = input("Please input a string:")
print("Your input string is:%s" % strings)
number, value = input("Input your change location number and changed value \
with Space bar separate:").split()
num_to_int = int(number)
string_to_list = list(strings)
string_to_list[num_to_int - 1] = value
print("Changed strings is: %s" % ''.join(string_to_list))
image.png
2.变量和简单的类型.jpeg
网友评论