effect : Transmit information
String expressions : A string of symbols enclosed in single or double quotation marks
The nature of strings
1. Strings are ordered ( Subscript 、 section )
2. Strings are immutable ( No addition, deletion or modification )
3. Container symbol of string :‘’,“”,‘’‘’‘’,“”“”“”; type :str
4. String classification : Ordinary character 、 Escape character 、 Original string
---- Symbols with special meanings
The original escape character requires the use of \ + Numbers represent symbols of special significance ; Now? c Language policy \ + Specific symbols denote symbols with special meanings
print('ab\tc\nd')print('ab\'cd')print('ab\\tcd')
ab c
d
ab’cd
ab\tcd
\ + Specifying symbols can make symbols with special meanings become themselves
It can make symbols without special meaning have special meaning
Add... Before the string R,r, The escape character in the string can be changed into itself
print('12\t34')print(r'12\t34')
12 34
12\t34
An escape character length is always 1, The escape length obtained by adding the original string is 2
print(len(r'\t'), len('\t'))
2 1
What you get is a new string object
print('ab' + 'cd')
abcd
print('*' * 20)
Follow the principle of comparing the size of the first pair of different elements
Python The coding table used is ASCII Expansion table of code table Unicode Encoding table ( unicode )
print('ab' > 'c') # ----> a and c Compare
False
chr() — Can convert the base to the corresponding symbol
ord() — Can convert symbols to decimal
print(ord('c'))print(ord('.'))print('ab.' > 'abc')print(chr(12290))
99
12290
True
.
notes :
A-Z The corresponding range : 65-90
a-z The corresponding range : 97-122
About Chinese :\u4e00 - \u9fa5 ( Hexadecimal ); 19968-40869( Decimal system )
print(chr(ord('\u4e00')), chr(ord('\u9fa5')))for i in range(19968, 40870): print(chr(i), end=' ')
( Part of the )
詇 Swearing 詉 詊 詋 詌 Babbler 讵 詏 Cheat I'm sorry No 詓 Imperial edict Comment on 詖 詗 Out of 詙 詚 Curse 詜 詝 Word 詟 Chant Boast Inquiry Attainments 詤 詥 Try 詧 詨 Poetry 詪 Surprise Criticize Weird Explain 詯 Ask Words The Detailed 詴 Shen Pay 詷 詸 Zhan Chen Oh, yeah Witty 詽 詾 Here 誀 誁 誂 誃 Eulogy Kill Cheat Exaggerate 誈 Reputation Transcribe 誋 Records Recognize 誎 Pay for 誐 Use EH Oath 誔 Birthday 誖 誗 Lure 誙 Scoff at 誛 She 誝 Language 誟 Cheng Commandment 誢 Frame By mistake Tomorrow Recite 誧 Teach 誩 Say 誫 Say A kind of 誮 誯 Who …
in、 not in
print('a' in 'abc')
True
Add
Binary system :0、1
example :A: Binary system :0b01000001, Decimal system :65, Hexadecimal :0x41, Octal symbols :0o101
print(chr(0b01000001), chr(65), chr(0x41), chr(0o101))
A A A A
oct() - Convert from hexadecimal to octal
print(hex(65))print(oct(65))
0x41
0o101
str1 = ''' Once upon a time, there was a temple on the mountain. There was an old monk in the temple. The old monk said to the little monk '''
Get the first one “ mountain ”, Subscript to be 5, Line feed is equivalent to a ’\n‘
print(str1[5])
mountain
" say " The subscript
print(str1[-2])
say
** Be careful :** A enter key is equivalent to a ’\n’, The length is 1
1. take “ Once upon a time there was a mountain ” Take out
print(str1[1: 6])
Once upon a time there was a mountain
2. take “ Old temple ”
print(str1[13:18:2])
Old temple
3. take “ There is a mountain in the temple seat ”
print(str1[-19: -24: -1])
There is a mountain in the temple seat
Add
How to wrap a complete line ?
num = 1 + 2 + \ 3 + 4 + 5 + 6 + 7 + 8 \ + 1 + 2 + 3 + \ 4 + 5 + 6 + \ 7 \ + \ 82 \ + 10print(num)str2 = '3245678' \ '9087654'print(str2)
156
32456789087654
str1 = ' Glory of Kings '
for i in str1: print(i)
king
person
Rong
Yao
for i in range(len(str1)): print(str1[i])# Get Wang Rong for i in range(0, len(str1), 2): print(str1[i])
king
person
Rong
Yao
king
Rong
practice
1.“abcderf”—>“ABCDERF”
str2 = 'abcderf'str3 = ''for i in str2: print(chr(ord(i)-32)) str3 += chr(ord(i)-32)print(str3)
ABCDERF
2.‘abcdABDCD One, two, three, four ’—>‘ABCDabcd One, two, three, four ’
str4 = 'abcdABDCD One, two, three, four 'str5 = ''for i in str4: if 'a' <= i <= 'z': str5 += chr(ord(i) - 32) elif 'A' <= i <= 'Z': str5 += chr(ord(i) + 32) else: str5 += iprint(str5)
ABCDabdcd One, two, three, four
cutting , Take the specified symbol as the cutting point , Separate the strings around the cutting point and save them in the list
print('abcAKAUHFAskfnalnla/s'.upper())
ABCAKAUHFASKFNALNLA/S
Capital to lowercase
print('abcAKAUHFAskfnalnla/s'.lower())
abcakauhfaskfnalnla/s
Capitalize the first letter in the string , A string that must start with a letter
print('abc123D456;.b'.capitalize())
Abc123d456;.b
cutting , Take the specified symbol as the cutting point , Separate the strings around the cutting point and save them in the list
result1 = '1, 2, 3'.split(',')print(result1)print('3' + '' == '3')
Abc123D456;.B
cutting , Take the specified symbol as the cutting point , Separate the strings around the cutting point and save them in the list
result1 = '1, 2, 3'.split(',')print(result1)print('3' + '' == '3')
[‘1’, ’ 2’, ’ 3’]
True
A sequence in which all elements are strings ( Containers ) All elements in the are spliced together with the specified symbols
result2 = ','.join(result1)print(result2)
1, 2, 3
By default, empty symbols at the beginning and end of the string are removed ( Space 、\n、\t And so on are empty symbols ), You can also specify the removed symbol
str1 = '\nabc \t\n'print('-' * 20)print(str1)print('-' * 20)print(str1.strip())print('-' * 20)print(str1.strip('\n'))print('-' * 20)print('abc--'.strip('.'))
Replace
replace(‘old_str’,‘new_str’,‘ frequency ’) — The specified number of in a string old_str Replace with new_str
str1 = ',1, 2, 3,'print(str1.replace(',', ' '))print(str1.replace(',', ' ', 2))
1 2 3
1 2, 3,
application
“ You are such a rubbish ”
– Lord 、 Predicate 、 Bin
–Python There's a module in jieba
[‘ you ’, ‘ can ’, ‘ It's ’, ‘ individual ’, ‘ The garbage ’]
– There is an existing Thesaurus ( Stop words ):
[‘ The garbage ’, ‘ Vegetable chicken ’, ‘ Spicy chicken ’]
– If jieba The result of word segmentation has appeared in the inactive thesaurus , Just replace it directly
str1 = " You are such a rubbish "stop_words = [' The garbage ', ' Vegetable chicken ', ' Spicy chicken ']str2 = ''list1 = [' you ', ' can ', ' It's ', ' individual ', ' The garbage ']for i in list1: if i in stop_words: str2 += str1.replace(i, '*'*len(i))print(str2)
You are such a **
author : Have a strong sense of self-management .
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .