自学Python01---字符串

自学Python01—字符串api

1.def capitalize(self)

首字母大写

# -*- coding: UTF-8 -*-

str = "you are beautiful"
print(str.capitalize())

# Result:
# You are beautiful
2.def center(self, width, fillchar=None)

将字符串填充至指定长度,位置居中,多余字符位用fillchar填充

# -*- coding: UTF-8 -*-

str = "nice"
result = str.center(10, "x")
print(result)

# Result:
# xxxnicexxx
3.def count(self, sub, start=None, end=None)

在指定区间内统计某字符串出现的次数

# -*- coding: UTF-8 -*-

str = "apple"
result = str.count("p", 0, 3)
print(result)

# Result:
# 2
4.def decode(self, encoding=None, errors=None)

以指定的编码格式解码

# -*- coding: UTF-8 -*-

str = "你好"
result = str.decode("gbk")
print(result)

# Result:
# 浣犲ソ
5.def encode(self, encoding=None, errors=None)

以指定编码格式编码

6.def endswith(self, suffix, start=None, end=None)

判断字符串在指定区间内的部分是否以suffix结尾

# -*- coding: UTF-8 -*-

str = "you are beautiful"
result1 = str.endswith("l", 0, str.__len__())
result2 = str.endswith("l", 0, str.__len__()-1)
print(result1)
print(result2)  

# Result
# True
# False
7.def expandtabs(self, tabsize=None)

返回字符串中的 tab 符号(‘\t’)转为空格后生成的新字符串,默认为8位

# -*- coding: UTF-8 -*-

str = "12\t34"
result = str.expandtabs()
print(str)
print(result)

# Result(观察空格数)
# 12    34
# 12      34
8.def find(self, sub, start=None, end=None)

在指定的区间内查找出sub在字符串中的位置,不存在返回-1

# -*- coding: UTF-8 -*-

str = "12345678"
result1 = str.find("34", 1, 5)
result2 = str.find("34", 3, 5)
print(result1)
print(result2)

# Result
# 2
# -1
9.def format(self, *args, **kwargs)

格式化字符串,通过{},:

>>> "{} {}".format("hello","world")
'hello world'

# 可以指定位置
>>> "{1} {0} {1}".format("hello","world")
'world hello world'

>>> print("白日{},黄河{}").format("依山尽","入海流")
白日依山尽,黄河入海流
# -*- coding: UTF-8 -*-

# 通过字典设置参数
site = {"name": "百度", "url": "www.baidu.com"}
print("网站名:{name}, 地址 {url}".format(**site))

# 通过列表索引设置参数
my_list = ['百度', 'www.baidu.com']
print("网站名:{0[0]}, 地址 {0[1]}".format(my_list))  # "0" 是必须的

# Result
# 网站名:百度, 地址 www.baidu.com
# 网站名:百度, 地址 www.baidu.com
10.def index(self, sub, start=None, end=None)

在指定的区间内查找出sub在字符串中的位置,不存在则返回异常

# -*- coding: UTF-8 -*-

str = "12345678"
result1 = str.index("45", 0, 8)
print(result1)

result2 = str.index("45", 0, 4)
print(result2)


# Result
# 3
# Traceback (most recent call last):
#   File "F:/Project/python_project/01_Python����/hm_01_hello.py", line 7, in <module>
#     result2 = str.index("45", 0, 4)
# ValueError: substring not found
11.def isalnum(self)

判断字符串是否是由字母或数字组成

# -*- coding: UTF-8 -*-

str1 = "abcd5678"
str2 = "abcd_5678"

result1 = str1.isalnum()
print(result1)
result2 = str2.isalnum()
print(result2)

# Result
# True
# False
12.def isalpha(self)

判断字符串是否是由字母组成

# -*- coding: UTF-8 -*-

str1 = "abcd"
str2 = "abcd5678"

result1 = str1.isalpha()
print(result1)
result2 = str2.isalpha()
print(result2)

# Result
# True
# False
13.def isdigit(self)

判断字符串是否全部由数字组成

# -*- coding: UTF-8 -*-

str1 = "12345"
str2 = "abcd5678"
result1 = str1.isdigit()
print(result1)

result2 = str2.isdigit()
print(result2)

# Result
# True
# False
14.def islower(self)

判断字符串中的字符是否都是小写字母

15.def isspace(self)

判断字符串是否由”空格”组成

# -*- coding: UTF-8 -*-

str1 = "12 45"
str2 = "    "
result1 = str1.isspace()
print(result1)

result2 = str2.isspace()
print(result2)

# Result
# False
# True
16.def istitle(self)

判断字符串是否符合英文标题的格式

# -*- coding: UTF-8 -*-

str1 = "You are beautiful"
str2 = "You Are Beautiful"
result1 = str1.istitle()
print(result1)

result2 = str2.istitle()
print(result2)

# Result
# False
# True
17.def isupper(self)

判断字符串是否由大写字母组成

18.def join(self, iterable)

将列表,元祖中的元素用字符串链接

# -*- coding: UTF-8 -*-

li = ["give", "me", "a", "pen"]
la = ("give", "me", "a", "pen")
result1 = "_".join(li)
print(result1)

result2 = "*".join(la)
print(result2)

# Result
# give_me_a_pen
# give*me*a*pen
19.def ljust(self, width, fillchar=None)

类似center方法,将字符串填充至指定长度,位置居,多余字符位用fillchar填充

20.def lower(self)

将字符串中所有的大写字母转换成小写字母

21.def lstrip(self, chars=None)

去除字符串开头的空格,或者去掉开头特定的chars字符串

# -*- coding: UTF-8 -*-

str1 = " 12345"
str2 = "xxxx12345"

result1 = str1.lstrip()
print(result1)
result2 = str2.lstrip("xxxx")
print(result2)


# Result
# 12345
# 12345
22.def partition(self, sep)

用sep将字符串切割成一个元组(包含sep)

# -*- coding: UTF-8 -*-

str = "alex SN SN alex"

result = str.partition("SN")
print(result)

# Result
# ('alex ', 'SN', ' SN alex')  注意,切割成了3份,而不是4份
23.def replace(self, old, new, count=None)

替换字符串中的字符

# -*- coding: UTF-8 -*-

result1 = "12345123121".replace("12", "xx")
result2 = "12345123121".replace("12", "xx", 1)
result3 = "12345123121".replace("12", "xx", 2)
print(result1)
print(result2)
print(result3)

# Result
# xx345xx3xx1
# xx345123121
# xx345xx3121
23.def rfind(self, sub, start=None, end=None)

从左向右截取指定区间的字符串,然后从右向左查找sub,返回第一个匹配的索引,不存在返回-1

24.def rindex(self, sub, start=None, end=None)

从左向右截取指定区间的字符串,然后从右向左查找sub,返回第一个匹配的索引,不存在返回异常

25.def rjust(self, width, fillchar=None)

类似center方法,将字符串填充至指定长度,位置居,多余字符位用fillchar填充

26.def rpartition(self, sep)

用sep将字符串(从右向左)切割成一个元组(包含sep)

# -*- coding: UTF-8 -*-

str = "alex SN SN alex"

result1 = str.partition("SN")
print(result1)
result2 = str.rpartition("SN")
print(result2)

# Result
# ('alex ', 'SN', ' SN alex')
# ('alex SN ', 'SN', ' alex')
27.def rsplit(self, sep=None, maxsplit=None)

用sep将字符串(从右向左)切割成一个列表(不包含sep)

# -*- coding: UTF-8 -*-

str = "alex SN SN alex"

result1 = str.rsplit(" ")
print(result1)
result2 = str.rsplit(" ", 1)
print(result2)

# Result
# ['alex', 'SN', 'SN', 'alex']
# ['alex SN SN', 'alex']
28.def rstrip(self, chars=None)

去除字符串结尾的空格,或者去掉结尾特定的chars字符串

29.def split(self, sep=None, maxsplit=None)

用sep将字符串(从左向右)切割成一个列表(不包含sep)

30.def splitlines(self, keepends=False)

根据字符串中的换行符(‘\r’, ‘\r\n’, \n’)切割字符串,组成一个列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。

# -*- coding: UTF-8 -*-

str = "hello\nworld"
print(str)
result1 = str.splitlines()
result2 = str.splitlines(True)
print(result1)
print(result2)

# Result
# hello
# world
# ['hello', 'world']
# ['hello\n', 'world']
31.def startswith(self, prefix, start=None, end=None)

判断字符串指定区间内的字符串是否以prefix开头

# -*- coding: UTF-8 -*-

str = "hello world"
result1 = str.startswith("e", 0, 4)
result2 = str.startswith("e", 1, 4)
print(result1)
print(result2)

# Result
# False
# True
32.def strip(self, chars=None)

去除字符串开头和结尾的空格,或者去掉开头和结尾特定的chars字符串

33.def swapcase(self)

将字符串中的字母的大小写反转

# -*- coding: UTF-8 -*-

str = "Hello World"
result = str.swapcase();
print(result)

# Result
# hELLO wORLD
34.def title(self)

将字符串格式化成英文标题格式

# -*- coding: UTF-8 -*-

str = "heLLo wOrld"
result = str.title();
print(result)

# Result
# Hello World
35.def upper(self)

将字符串中所有的大写字母转换成小写字母

36.def zfill(self, width)

返回长度为 width 的字符串,原字符串右对齐,前面填充0


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值