python中字符串的各种处理(未完待续)

字符串的定义:
由一对单引号或双引号引起来的内容即是字符串。

类型:
字符串是不可变类型(无论对字符串进行那种操作,字符串本身是不会改变的,返回的是被操作后新的字符串对象)。

操作字符串的方法:

1,字符串[]
语法:
字符串[要获取的子字符串的下标]
作用:
获取字符串指定下标的子字符串

示例A:

string = "first set a small target, i first earn it a billion"
index = string[3]
print(index)
console:s

2,len()方法

语法:
len(要操作的字符串)
作用:
返回字符串的长度。

例子A:

string = "first set a small target, i first earn it a billion"
length = len(string)
print(length)
 console51

3,coun()方法

语法:
字符串.count(子字符串)
作用:
返回子字符串在字符串中出现的次数,如果没有出现,则返回数字0.

示例A:

string = "first set a small target, i first earn it a billion"
count = string.count("s")
print(count)
console4

示例B:

string = "first set a small target, i first earn it a billion"
count = string.count("what?")
print(count)
console0

4,index()方法

语法:
字符串.index(要查找的子串,起始下标,结尾下表)
当然,后两个参数可以不声明。
作用:查找指定的子字符串,如果找到则返回子串的下标(索引),
如果没找到则报错。

示例A:

# 查找“ar”第一次出现的下标。
string = "first set a small target, i first earn it a billion"
index = string.index("ar", 19)
print(index) 
console19

示例B:

# 查找“ar”除去第一次后,出现的下标。
string = "first set a small target, i first earn it a billion"
index = string.index("ar", 20)
print(index)
console35
# 看一下没有找到的情况
string = "first set a small target, i first earn it a billion"
index = string.index("Bigbang")
print(index)
console:
Traceback (most recent call last):
  File "D:/Python/PycharmProjects/happy time/new_text.py", line 3, in <module>
    index = string.index("Bigbang")
ValueError: substring not found

5,rindex()方法

语法:
用法和index()方法一样,不过从右边开始查找。

示例A:

# 查找“ar”最后一次出现的下标。
string = "first set a small target, i first earn it a billion"
index = string.rindex("ar")
print(index)
console35

6,find()方法

(跟index方法一样)
字符串.find(要查找的子串,起始下标,结尾下表)
当然,后两个参数可以不声明。
作用:
查找指定的子字符串,但与index方法不同的是如果找到则返回子串的下标(索引),如果没找到则返回-1。(实际应用中find方法更常用)
示例A:

string = "first set a small target, i first earn it a billion"
count = string.find("whf")
print(count)
console:-1

7,rfind()方法

语法:
用法和rfind()方法一样,不过从右边开始查找。

示例A:

string = "first set a small target, i first earn it a billion"
count = string.rfind("a")
print(count
console42

8,isdigit()方法

语法:
字符串.isdigit()
作用:
判断整个字符串是否全部为整数(不是数字),如果是则返回True,否则返回False。

示例A:

string = "first set a small target, i first earn it a billion"
count = string.isdigit()
print(count)
console :False

示例B:

# 字符串是整数的情况
string2 = "456789"
red = string2.isdigit()
print(red)
console: True

示例C:

# 字符串是小数的情况
string2 = "456789.67"
red = string2.isdigit()
print(red)
console:False

9,isalpha()方法

语法:
字符串.isalpha()
作用:
判断字符串是否为纯数字组成,如果是则返回True,否则返回False。

示例A:

string = "wtf"
count = string.isalpha()
print(count)
console:True

示例B:

string = "first set a small target, i first earn it a billion"
count = string.isalpha()
print(count)
console: False

10,isspace()方法

语法:
字符串.isspace()
作用:
判断字符串是否以纯空格组成,如果是,则返回True,否则返回False。
示例A:

string = "  "
count = string.isspace()
print(count)
console:True

示例2:

string = "first set a small target, i first earn it a billion"
count = string.isalpha()
print(count)
console:False

11,replace()函数

语法:
字符串.replace(老子字符串,新子字符串,替换的次数)
作用:
通过replace方法将字符串中指定的老子字符串替换为新的子字符串,如果不指定替换的次数,则默认全部替换。

示例A:

string = "first set a small target, i first earn it a billion"
count = string.replace("a", "A")
print(count)
console:

first set A smAll tArget, i first eArn it A billion

示例B:

#  指定替换的次数
string = "first set a small target, i first earn it a billion"
count = string.replace("a", "A", 2)
print(count)
console:

first set A smAll target, i first earn it a billion

12,startswith()方法

语法:
字符串.startswith(“子字符串”)
作用:
判断字符串是否由指定的子字符串开头,是则返回True,否则返回False

示例A:

string = "first set a small target, i first earn it a billion"
count = string.startswith("first")
print(count)
console:True

示例B:

string = "first set a small target, i first earn it a billion"
count = string.startswith("friss")
print(count)
console:False

13,endswith()方法

语法:
字符串.endswith(“子字符串”)
作用:
判断字符串是否由指定的子字符串结尾,是则返回True,否则返回False

示例A:

string = "first set a small target, i first earn it a billion"
count = string.endswith("billion")
print(count)
console:True

示例B:

string = "first set a small target, i first earn it a billion"
count = string.endswith("billoon")
print(count)
console:False

14,strip()函数

语法:
字符串.strip()
作用:
去掉字符串两边的空格。

示例A:

string = "  first set a small target, i first earn it a billion  "
count = string.strip()
print(count)
print(string)
console:
first set a small target, i first earn it a billion
  first set a small target, i first earn it a billion  

另外:
lstrip()和rstrip()函数,他们的语法同strip(),只不过一个是去掉左边的空格,一个是去掉右边的空格,这里不再赘述。


15,split()函数

语法:
字符串.split(分割符,分割的次数)
作用:
将字符串按分割符分割,(丢失分割符),如果 分割的次数有指定值,则仅分隔 分割次数 + 1 个子字符串,并返回一个列表。

示例:

string = "  first set a small target, i first earn it a billion  "
count = string.split(" ")
print(count)
console:
['', '', 'first', 'set', 'a', 'small', 'target,', 'i', 'first', 'earn', 'it', 'a', 'billion', '', '']

示例B:

#  指定分割次数
string = "  first set a small target, i first earn it a billion  "
count = string.split(" ", 5)
print(count)
['', '', 'first', 'set', 'a', 'small target, i first earn it a billion  ']

16,partition()函数

语法:
字符串.partition(子字符串)
作用:
通过partition()函数,根据子字符串,把字符串分割为三部分,子字符串前,子字符串,子字符串后,返回一个元祖,

示例A:

string = "  first set a small target, i first earn it a billion  "
count = string.partition("target")
print(type(count))
print(count)
console:
<class 'tuple'>
('  first set a small ', 'target', ', i first earn it a billion  ')

未完待续。。。。。。

Guess you like

Origin blog.csdn.net/James_Nan/article/details/78163184