Python中没有字符类型的说法,只有字符串,这里所说的字符就是只包含一个字符的字符串!!!这里这样写的原因只是为了方便理解,仅此而已. 1. 按照某一个分隔符分割一个字符串: >>> str = "my name is liu de hua" >>> str 'my name is liu de hua' >>> split_str = str.split(' ') >>> print split_str ['m…
1.1 python字符串定义 #!/usr/bin/python # -*- coding: utf8 -*- # 定义一个字符串 s1 = 'this is long String that spans two lines' # 表示下面一行是上一行的延续 s2 = 'this is long String\ that spans two lines' #原样输出字符串 s3 = """this is long String that spans two lines &q…
# pattern支持字符或者字符串 def my_split(string, pattern): ret = [] len_pattern = len(pattern) while True: index = string.find(pattern) if index == -1: ret.append(string) return ret else: ret.append(string[:index]) string = string[index+len_pattern:] if __nam…
题目:首先生成包含1000个随机字符的字符串,然后统计每个字符的出现次数 import string import random x = string.ascii_letters + string.digits + string.punctuation # print(x) y = [random.choice(x) for i in range(10)] #生成包含1000个随机字符的字符串 # print(y) d = dict() #使用字典保存每个字符出现次数 for ch in y:…