Python - string

(1) string

    Strings are the most commonly used data type in Python. We can use quotation marks ( ' or " ) to create strings. As the name suggests, kebabs are strings made of mutton, and strings are made up of characters. The essence of strings is: a sequence of characters    .

2     Python does not support single-character types, and single characters are also used as a string. Creating a string is as simple as assigning a value to a variable. For example:
var1 = 'Hello World!'
var2 = "Python"
    Python strings are immutable, we cannot make any changes to the original string. But it is possible to copy part of the string to the newly created string to achieve the "look modified " effect.

(2) Encoding of strings

   Python3 directly supports Unicode , which can represent characters of any written language in the world. Python3 characters default to 16 -bit Unicode encoding, and ASCII code is a subset of Unicode encoding.

Conversion Tips: 

Use the built-in function ord() to convert characters into corresponding Unicode codes;
Use the built-in function chr() to convert decimal numbers into corresponding characters;

 

print(ord('A')) #65
print(ord('成')) #25104
print(chr(66)) #'B'
print(ord('龙') )#40857

(3) Quotation marks create a string

   We can create strings by ①single quotes or double quotes. For example: a='abc' b="sxt" The advantage of using two kinds of quotation marks is that you can create a string that contains quotation marks itself without using escape characters. For example:
a = "I'm a teacher!"
print(a) #I'm a teacher!
b = 'my_name is "Jackie Chan"'
print(b) #my_name is "Jackie Chan"
   ② Three consecutive single quotes or three double quotes can help us create multi-line strings. In long strings the original formatting is preserved. For example:

(4) Empty string and len() function

   Python allows the existence of empty strings, which contain no characters and have a length of 0 . For example:

 

c = ''
print(len(c)) #结果:0
  Among them, len() is used to calculate how many characters the string contains. 

(5) Escape characters

     We can use \+ special characters to achieve some effects that are difficult to express with characters. For example: newline, etc. Common escape characters are:
escape character describe
\ (at the end of the line) continuation character
\\ backslash symbol
\' apostrophe
\" Double quotes
\a ring the bell
\b Backspace
\e escape
\000 null
\n new line
\v vertical tab
\t horizontal tab
\r carriage return
\f change page
\oyy Octal number, y stands for characters from 0 to 7, for example: \012 stands for newline.
\xyy Hexadecimal number, starting with \x, the character represented by yy, for example: \x0a represents newline
\other Other characters are output in normal format
[Operation] Test the use of escape characters

the code

a = 'I\nlove\bU'
print(a)
print('a\rabb\\cc')

 

(6) Python string operators

The value of instance variable a in the following table is the string "Hello", and the value of variable b is "Python":

operator describe example
+ string concatenation

>>>a + b 'HelloPython'

* Repeat output string

>>>a * 2 'HelloHello'

[] Get characters in a string by index

>>>a[1] 'e'

[ : ] Extract part of a string

>>>a[1:4] 'ell'

in Membership operator - returns True if the given character is contained in the string

>>>"H" in a True

not in Membership operator - returns True if the given character is not contained in the string

>>>"M" not in a True

r/R Raw Strings - Raw Strings: All strings are used literally, without escaping special or unprintable characters. Raw strings have almost the same syntax as normal strings, except that the letter "r" (upper and lower case) is added before the first quotation mark in the string.

>>>print r'\n' \n >>> print R'\n' \n

% format string see next chapter

 specific code

a = "Hello"
b = "Python"
print("a + b 输出结果:", a + b)
print("a * 2 输出结果:", a * 2)
print("a[1] 输出结果:", a[1])
print("a[1:4] 输出结果:", a[1:4])

if ("H" in a):
    print( "H 在变量 a 中")
else:
    print( "H 不在变量 a 中")

if ("M" not in a):
    print("M 不在变量 a 中")
else:
    print("M 在变量 a 中")

print(r'\n')
print(R'\n')

running result:

String splicing : You can use + to splice multiple strings together. Example: 'aa'+'bb' results in 'aabb'
  •  If both sides of the + are strings, concatenate.

  •  Addition if both sides of + are numbers

  • If the types of the two sides of the + are different, an exception is thrown

 You can concatenate multiple literal strings directly together. Example : 'aa' 'bb' results in 'aabb'

 

a = 'sxt'+'qiqi'   #结果是:'sxtqiqi'
b = 'sxt''qiqi'     #结果是:'sxtqiqi'

(7) String slice slice operation

 

The  slice operation allows us to quickly extract substrings. The standard format is:
   [start offset start: end offset end: step size step]

 

Typical operations (when the three quantities are positive numbers) are as follows:
Operation and instructions example result
[:] extracts the entire string “abcdef”[:] "abcdef"
[start:] from the start index to the end “abcdef”[2:] “cdef”
[:end] Know end-1 from scratch “abcdef”[:2] “ab”
[start:end] from start to end-1 “abcdef”[2:4] “cd”
[start:end:step] Extract from start to end-1 , the step size is step “abcdef”[1:5:2] “bd”

Cases for other operations (three quantities are negative)
example illustrate result
"abcdefghijklmnopqrstuvwxyz"
[-3:]
last three “xyz”
"abcdefghijklmnopqrstuvwxyz"
[-8:-3]
The eighth from the last to the third from the last ( include header but not tail )
'stuvw'
"abcdefghijklmnopqrstuvwxyz"
[::-1]
Negative step size, reverse extraction from right to left
'zyxwvutsrqponmlkjihgfedcba'

注意:切片操作时,起始偏移量和终止偏移量不在[0,字符串长度-1]这个范围,也不会报错。起始偏移量小于0则会当做0,终止偏移量大于长度-1”会被当成-1。例如:

 

 

(八)split()分割和join()合并

      split() 可以基于指定分隔符将字符串分隔成多个子字符串 ( 存储到列 表中) 。如果不指定分隔符,则默认使用空白字符 ( 换行符 / 空格 / 制表符) 。示例代码如下:
a = "to be or not to be"
print(a.split())
print(a.split('be'))
运行效果:

join() 的作用和 split() 作用刚好相反,用于将一系列子字符串连接起
来。示例代码如下:
a = ['sxt','sxt100','sxt200']
print('*'.join(a))

运行效果: 

总结:

拼接字符串要点:
      使用字符串拼接符 + ,会生成新的字符串对象,因此不推荐使 用 + 来拼接字符串。推荐使用 join 函数,因为 join 函数在拼接字 符串之前会计算所有字符串的长度,然后逐一拷贝,仅新建一次对象。

 

 

(九) 字符串常用方法汇总

       字符串有很多常用的方法,我们需要熟悉。我们通过表格将这些方 法汇总起来,方便大家查阅。希望大家针对每个方法都做一次测试。

 

我们以一段文本作为测试:
 
a='''ablchbjj '''
常用查找方法
方法和使用示例 说明 结果
len(a) 字符串长度 8
a.startswith('abl') 以指定字符串开头 True
a.endswith('jj') 以指定字符串结尾 True
a.find('b') 第一次出现指定字符串的位置 1
a.rfind('b') 最后一次出现指定字符串的位置 5
a.count("lch") 指定字符串出现了几次 1
a.isalnum() 所有字符全是字母或数字 True
 

Guess you like

Origin blog.csdn.net/qq_63976098/article/details/131506481