<jupyter_start><jupyter_text>All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures  字符串Strings 字符串表示一段文字，用配对的单引号或双引号表示。<jupyter_code>String0 = '<NAME> is beautiful'
String1 = "<NAME> is beautiful"
String2 = '''<NAME>
is
beautiful'''
print(String0 , type(String0))
print(String1, type(String1))
print(String2, type(String2))<jupyter_output>T<NAME> is beautiful <class 'str'>
T<NAME>ahal is beautiful <class 'str'>
T<NAME>ahal
is
beautiful <class 'str'><jupyter_text>字符串的序号索引和切片索引与列表类似：<jupyter_code>print(String0[4])
print(String0[4:])<jupyter_output>M
Mahal is beautiful<jupyter_text>字符串的内置函数 **find( )** 返回指定元素的序号索引。 如果没找到返回 **-1**.<jupyter_code>print(String0.find('al'))
print(String0.find('am'))<jupyter_output>7
-1<jupyter_text>注意，此处7返回的是要查找的'al'的第一个字符a的序号.<jupyter_code>print(String0[7])<jupyter_output>a<jupyter_text>**find( )** 还可以在指定的序号范围内查找：<jupyter_code>print(String0.find('j',1))
print(String0.find('j',1,3))<jupyter_output>2
2<jupyter_text>**capitalize( )** 将首字母大写：<jupyter_code>String3 = 'observe the first letter in this sentence.'
print(String3.capitalize())<jupyter_output>Observe the first letter in this sentence.<jupyter_text>**center( )** 根据指定的宽度将字符串内容居中排列：<jupyter_code>String0.center(70)<jupyter_output><empty_output><jupyter_text>也可以指定填充左右空白的符号：<jupyter_code>String0.center(70,'-')<jupyter_output><empty_output><jupyter_text>**zfill( )** 用0来填补前面的空白.<jupyter_code>String0.zfill(30)<jupyter_output><empty_output><jupyter_text>**expandtabs( )** 可以改变制表符的占位宽度. 制表符'\t' 默认宽度为8.<jupyter_code>s = 'h\te\tl\tl\to'
print(s)
print(s.expandtabs(1))
print(s.expandtabs())<jupyter_output>h	e	l	l	o
h e l l o
h       e       l       l       o<jupyter_text>**index( )** 用法与 **find( )** 类似，唯一的区别是，find找不到元素的时候返回-1，而index找不到元素的时候会报错。<jupyter_code>print(String0.index('Taj'))
print(String0.index('Mahal',0))
print(String0.index('Mahal',10,20))<jupyter_output>0
4<jupyter_text>**endswith( )** 判断一个字符串是否以指定的字符结尾：<jupyter_code>String0.endswith('y')<jupyter_output><empty_output><jupyter_text>判断的范围也可以指定：<jupyter_code>print(String0.endswith('l',0))
print(String0.endswith('M',0,5))<jupyter_output>True
True<jupyter_text>**count( )** 统计特定元素在字符串中出现的次数，范围也可指定<jupyter_code>print(String0.count('a',0))
print(String0.count('a',5,10))<jupyter_output>4
2<jupyter_text>**join( )** 在每一个字符之间插入指定字符：<jupyter_code>'a'.join('*_-')<jupyter_output><empty_output><jupyter_text>'*_-' 中每个符号间都插入了一个字符 'a'  因此 **join( )** 可以用来把一个列表转化为一个字符串.<jupyter_code>a = list(String0)
print(a)
b = ''.join(a)
print(b)<jupyter_output>['T', 'a', 'j', ' ', 'M', 'a', 'h', 'a', 'l', ' ', 'i', 's', ' ', 'b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l']
Taj Mahal is beautiful<jupyter_text>**join( )** 也可以插入任意字符<jupyter_code>c = '/'.join(a)[18:]
print(c)<jupyter_output>/i/s/ /b/e/a/u/t/i/f/u/l<jupyter_text>**split( )** 可以把一个字符串拆成列表. 是对 **join()** 的反向操作.<jupyter_code>d = c.split('/')
print(d)<jupyter_output>[' ', 'i', 's', ' ', 'b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l']<jupyter_text>**split( )** 还可以指定分解的次数，例如拆分3次：<jupyter_code>e = c.split('/',3)
print(e)
print(len(e))<jupyter_output>[' ', 'i', 's', ' /b/e/a/u/t/i/f/u/l']
4<jupyter_text>**lower( )** 将一个字符串里的所有大写转化为小写：<jupyter_code>print(String0)
print(String0.lower())<jupyter_output>Taj Mahal is beautiful
taj mahal is beautiful<jupyter_text>**upper( )** 将所有小写转为大写：<jupyter_code>String0.upper()<jupyter_output><empty_output><jupyter_text>**replace( )** 用其他元素替代指定元素：<jupyter_code>String0.replace('Taj Mahal','Bengaluru')<jupyter_output><empty_output><jupyter_text>**strip( )** 可以去除左右两边指定的元素，例如<jupyter_code>f = '    hello      '<jupyter_output><empty_output><jupyter_text>如果不指定，则删除左右两边的空格<jupyter_code>f.strip()<jupyter_output><empty_output><jupyter_text>**strip( )** 指定删除的元素，例如要删掉星号：<jupyter_code>f = '   ***----hello---*******     '
f.strip('*')<jupyter_output><empty_output><jupyter_text>但是可以看到星号并没有被删掉，这是因为星号的两边还有空格，strip是要按照顺序来去除左右的元素的，例如我们指定先删空格再删星号：<jupyter_code>print(f.strip(' *'))
print(f.strip(' *-'))<jupyter_output>----hello---
hello<jupyter_text>**lstrip( )** 和 **rstrip( )** 就是分别从左边或者右边删除指定元素：<jupyter_code>print(f.lstrip(' *'))
print(f.rstrip(' *'))<jupyter_output>----hello---*******     
   ***----hello---<jupyter_text>字典Dictionaries Dictionaries字典多用来当做一个数据库来使用，因为可以通过索引来访问对应的值。 定义字典可以用 { } 或 dict()<jupyter_code>d0 = {}
d1 = dict()
print(type(d0), type(d1))<jupyter_output><class 'dict'> <class 'dict'><jupyter_text>字典和列表很像，但字典可以指定一个特殊的索引，而列表只能用整数来做索引，例如：<jupyter_code>d0['One'] = 1
d0['OneTwo'] = 12 
print(d0)<jupyter_output>{'OneTwo': 12, 'One': 1}<jupyter_text>但首先要注意的是：* 1. 字典的索引，也就是键不能有重复值；* 2. 字典内的元素没有顺序；<jupyter_code>d1={'One':1,'Two':2,'Three':3,'Four':4,'One':11}
print(d1)<jupyter_output>{'Two': 2, 'One': 11, 'Three': 3, 'Four': 4}<jupyter_text>字典的键可以是任意的“hashable”的类型（简单来说，就是需要具有__hash__()方法）：<jupyter_code>l1 = [0,1]
l2 = [2,3,4]
d1 = {l1:'length 2',l2:'length 3'}<jupyter_output><empty_output><jupyter_text>可以看出，list列表不是一个hashable的类型。相对应地，tuple元组就是一个hashable的类型：<jupyter_code>l1 = (0,1)
l2 = (2,3,4)
d1 = {l1:'length 2',l2:'length 3'}
print(d1)<jupyter_output>{(0, 1): 'length 2', (2, 3, 4): 'length 3'}<jupyter_text>自定义的任何一个类的不同实例，默认都是hashable的：<jupyter_code>class UserDefined():
    pass

ud1 = UserDefined()
ud2 = UserDefined()

d1 = {ud1:'length 2',ud2:'length 3'}
print(d1)<jupyter_output>{<__main__.UserDefined object at 0x7fe5680da588>: 'length 2', <__main__.UserDefined object at 0x7fe5680da5c0>: 'length 3'}<jupyter_text>现在就可以用指定的索引“one”这个字符串来访问指定的值：<jupyter_code>print(d0['One'])<jupyter_output>1<jupyter_text>两个相关的列表可以合成一个字典，比如以下例子，把names列表里的元素当做键，把numbers列表里的内容当做值：<jupyter_code>names = ['One', 'Two', 'Three', 'Four', 'Five']
numbers = [1, 2, 3, 4, 5]<jupyter_output><empty_output><jupyter_text>**zip( )** 函数可以合并两个列表：<jupyter_code>d2 = zip(names,numbers)
print(d2)<jupyter_output><zip object at 0x7fe5689ba988><jupyter_text>d2是一个zip形式的列表，每个元素都是一个两个元素的元组，这两个元素分别来自names和numbers接下来就可以用 **dict( )** 函数把d2转化为字典：<jupyter_code>a1 = dict(d2)
print(a1)<jupyter_output>{'Two': 2, 'One': 1, 'Three': 3, 'Five': 5, 'Four': 4}<jupyter_text>字典的内置函数 **clear( )** 清除所有数据：<jupyter_code>a1.clear()
print(a1)<jupyter_output>{}<jupyter_text>字典还可以通过循环来创建：<jupyter_code>### 以下循环遍历了names和numbers，将他们逐一配对赋值给一个字典a1
for i in range(len(names)):
    a1[names[i]] = numbers[i]
print(a1)<jupyter_output>{'Two': 2, 'One': 1, 'Three': 3, 'Five': 5, 'Four': 4}<jupyter_text>**values( )** 返回一个由字典中的各个值组成的列表：<jupyter_code>a1.values()<jupyter_output><empty_output><jupyter_text>**keys( )** 返回一个由字典中的各个键组成的列表：<jupyter_code>a1.keys()<jupyter_output><empty_output><jupyter_text>**items( )** 返回字典中各个键-值对的组成的列表，在这个列表中，每一个元素是一对键和值组成的元组：<jupyter_code>a1.items()<jupyter_output><empty_output><jupyter_text>**pop( )** 可以取出一个值再赋值给别的变量. 但是赋值的只有值，没有键：<jupyter_code>a2 = a1.pop('Four')
print(a1)
print(a2)<jupyter_output>{'Two': 2, 'One': 1, 'Three': 3, 'Five': 5}
4
{'fraction_non_alphanumeric': 0.20777323202805376, 'fraction_numerical': 0.031268264172998245, 'mean_word_length': 10.408333333333333, 'pattern_counts': {'":': 0, '<': 159, '<?xml version=': 0, '>': 159, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '43', 'dir': 'jupyter-structured-clean-dedup', 'id': '427603', 'license': 'MIT', 'n_tokens_mistral': 3804, 'n_tokens_neox': 3891, 'n_words': 338, 'path': 'notebooks/python_tutorials_04.ipynb', 'repo_name': 'GimpelZhang/git_test', 'size': '6851'}