python 基础数据类型之字符串02

1、字符串去除空格

#
# strip(self, chars=None) #去除字符串两端空格
# lstrip(self, chars=None) #去除字符串左端空格
# rstrip(self, chars=None) #去除字符串右端空格
程序:
str1 = "  hello world!  "
print str1.strip()
print str1.lstrip()
print str1.rstrip()
运行结果:
hello world!
hello world!  
  hello world!

2、字符串中的大小写转换

#
# lower(self) #全转换为小写
# upper(self) #全转换为大写
# swapcase(self) #大小写互换
# capitalize(self) #只有字符串首字母大写,其余都小写
# title(self) #单词首字母转换为大写
程序
str2 = "hello World!"
print str2.lower()
print str2.upper()
print str2.swapcase()
print str2.capitalize()
print str2.title()
运行结果:
hello world!
HELLO WORLD!
HELLO wORLD!
Hello world!
Hello World!

3、查找字符串位置

#
# find(self, sub, start=None, end=None) 从左边查找字符串第一位置,找不到返回-1,找到返回索引位置
# index(self, sub, start=None, end=None) 从左边查找字符串第一位置,找不到报错,找到返回索引位置
# rfind(self, sub, start=None, end=None) 从右边开始查找字符串第一位置,找不到返回-1,找到返回索引位置
# rindex(self, sub, start=None, end=None) 从右边查找字符串第一位置,找不到报错,找到返回索引位置
程序:
str3 = "hello world!"
print str3.find("w", 0, 3,)
print str3.index("w", 0, 7,)
print str3.rfind("l", 0, 7,)
print str3.rindex("l", 0, 7,)
运行结果:
-1
6
3
3

4、字符串对齐

#
# rjust(self, width, fillchar=None) 取固定长度右对齐,左边不够空格补齐
# ljust(self, width, fillchar=None) 取固定长度左对齐,右边不够空格补齐
# center(self, width, fillchar=None)取固定长度中间对齐,左右不够用空格补齐
程序:
str4 = "hello world!"
print str4.rjust(20, "-")
print str4.ljust(20, "+")
print str4.center(20, "~")
运行结果:
--------hello world!
hello world!++++++++
~~~~hello world!~~~~

5、bool判断

#
# isspace(self) 字符串是否为空格
# isupper(self) 字符串是否全大写
# islower(self) 字符串是否全小写
# isalnum(self) 是否全为字母或数字
# isalpha(self) 是否全字母
# isdigit(self) 是否全数字
# isspace(self) 是否是标题
# startswith(self, prefix, start=None, end=None) 是否已某字符串开头
# endswith(self, suffix, start=None, end=None) 是否已某字符串结尾
程序:
str5 = "hello"
print str5.isspace()
print str5.islower()
print str5.startswith(" ")
print str5.endswith("!")
print str5.isalpha()
print str5.isalnum()
print str5.isalpha()
print str5.isdigit()
print str5.istitle()
运行结果:
False
True
False
False
True
True
True
False
False

6、字符串分割

#
# split(self, sep=None, maxsplit=None) 按照某符号分割,次数。
# rsplit(self, sep=None, maxsplit=None) 按照某符号从右侧开始分割,次数。
# partition(self, sep: str) 字符串包含指定的分隔符,则返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。
# rpartition(self, sep: str) 类似于 partition()函数,不过是从右边开始查找.
程序:
str6 = "www.baidu.com"
print str6.split(".")
print str6.split(".", 1)
print str6.rsplit(".", 1)
str = "http://www.baidu.//com"
print str.partition("//")
print str.rpartition("//")
运行结果:
[‘www‘, ‘baidu‘, ‘com‘]
[‘www‘, ‘baidu.com‘]
[‘www.baidu‘, ‘com‘]
(‘http:‘, ‘//‘, ‘www.baidu.//com‘)
(‘http://www.baidu.‘, ‘//‘, ‘com‘)

7、字符串连接

#
# join(self, iterable) 连接(可迭代的)元组或列表中的元素,用某字符
程序:
list1 = ("hello", "world",)
str7 = "~"
print str7.join(list1)
运行结果:
hello~world

8、字符串计数

#
# count(self, x, start, end,) 某个字符串出现的次数,开始结束位置
程序:
str8 = "hello world!"
print str8.count("l", 0, 6, )
运行结果:
2

9、tab键填充空格、tab用\t表示

#
# expandtabs(self, tabsize=None)把字符串中的 tab 符号(‘\t‘)转为空格,tab 符号(‘\t‘)默认的空格数是 8
程序:
str9 = "hello\t\tworld! "
print str9.expandtabs()
运行结果:
hello           world!

10、字符串替换/格式化

#
# format(self, *args, **kwargs) 替换占位符 {0} {1} ...
# replace(self, old, new, count=None) 替换指定次数的old为new,从左开始
程序:
str10 = "hello world"
print ("name:{0} age:{1}".format("hello", "10"))
print ("hello:{name},world:{age}".format(name="yang", age="20"))
str11 = "hello hello hello!"
print str11.replace("hello", "HELLO", 2)
运行结果:
name:hello age:10
hello:yang,world:20
HELLO HELLO hello!

11、zfill()

#
# zfill(self, width: int) 方法返回指定长度的字符串,原字符串右对齐,前面填充0。
程序:
str12 = "yang"
print str12.zfill(20)
运行结果:
0000000000000000yang
#
# def decode(self, encoding: unicode = ..., errors: unicode = ...) -> unicode: ...
# def encode(self, encoding: unicode = ..., errors: unicode = ...) -> str: ...
# def splitlines(self, keepends: bool = ...) -> List[str]: ...
# def translate(self, table: Optional[AnyStr], deletechars: AnyStr = ...) -> AnyStr: ...
时间: 2024-07-30 04:13:36

python 基础数据类型之字符串02的相关文章

Python基础数据类型之字符串

Python基础数据类型之字符串 一.Python如何创建字符串 在python中用引号将一些文本包起来就构成了字符串(引号可以是单引号.双引号.单三引号,双三引号,它们是完全相同的) >>> str1 = 'hello' >>> str2 = "hello" >>> str3 = '''hello''' >>> str4 = """hello""" &g

python基础数据类型----整数 ,字符串【常用操作方法】,布尔值,for循环

Python基础数据类型(4.29) bool str int 三者之间的转换 str索引切片,常用操作方法 for循环(大量的练习题) 1.基础数类型总览 整数(int) ,字符串(str),布尔值(bool),列表(list),元组(tuple),字典(dict),集合(set). 10203 123 3340 int 主要用于计算+- * / 等等 '今天吃了没?' str 存储少量的数据,并进行相应的操作.str1 + str2, str *int , 索引,切片, 其他操作方法 Tru

python基础数据类型补充以及编码进阶

01 内容大纲 基础数据类型的补充 数据类型之间的转换 编码的进阶 02 具体内容: 数据类型的补充: str # str :补充的方法练习一遍就行. s1 = 'taiBAi' # capitalize 首字母大写,其余变小写 print(s1.capitalize()) # swapcase 大小写翻转 print(s1.swapcase()) # title 每个单词的首字母大写 msg= 'taibai say3hi' print(msg.title()) s1 = 'barry' #

Python基础数据类型考试题

# Python基础数据类型考试题 # 考试时间:两个半小时 满分100分(80分以上包含80分及格) # 一,基础题. # 1, 简述变量命名规范(3分) # 1.变量由字母.数字.下划线任意组成 # 2.不能以数字开头 # 3.不能使用python关键字 # 4.变量要具有可描述性 # 5.变量不能是中文 # 5.官网推荐骆峰体和下划线,这里推荐下划线 # 2,字节和位的关系.(2分) # 1字节 = 8位 # 3,'太白'使用utf-8编码时,占的位数和字节数,是多少?使用gbk编码时,占

Python基础数据类型题

Python基础数据类型 题考试时间:三个小时 满分100分(80分以上包含80分及格)1,简述变量命名规范(3分) 1.必须是字母,数字,下划线的任意组合. 2.不能是数字开头 3.不能是python中的关键字 4.变量不能是中文 5.变量不能过长 6, 变量要具有可描述性 2,字节和位的关系.(2分)8位等于1个字节3,'A太白'使用utf-8编码时,占的位数和字节数,是多少?使用gbk编码时,占的位数和字节数,是多少.(2分)'A太白'使用utf-8编码时,占56位和7个字节,使用gbk编

2 Python基础数据类型

Python基础数据类型 # 数据类型分为可变数据类型和不可变数据类型 # 可变数据类型:list.set.dict 不可哈希 # 不可变数据类型:str.int.bool.tuple(元祖) 可哈希 基本类型和数据集 基本类型 int 数字型:int 1,2,3,56 取值范围为:-2\*\*31~2**31-1 可以进行的运算:+ - * / **(幂次方) %(取余) type() 查看数据类型 str 字符串:str python中只要是用引号引起来的全是字符串 字符串转化成数字的条件:

python基础数据类型补充以及编码的进阶

一. 基础数据类型补充内容 1.1 字符串 字符串咱们之前已经讲了一些非常重要的方法,剩下还有一些方法虽然不是那么重要,但是也算是比较常用,在此给大家在补充一些,需要大家尽量记住. #captalize :首字母大写 #swapcase :大小写翻转 #title :每个单词的首字母大写 #center :内同居中,总长度,空白处填充 #寻找字符串中的元素是否存在 #find :返回的找到的元素的索引,如果找不到返回-1 #index :返回的找到的元素的索引,找不到报错. #captalize

python基本数据类型之字符串(三)

python基本数据类型之字符串(三) 转换和判断方法 在python中,有一些内置方法可以将字符串转化特定形式,而与之对应的一些方法可以判断字符串是否符合某些形式.因此,在这篇文章中,笔者把转换方法和相应的判断方法放在一起进行讲解. 这些方法包括:capitalize.casefold.lower\islower.upper\isupper.maketrans\translate.swapcase.title\istitle 1.capitalize.title.istitle capital

python基本数据类型之字符串(四)

python基本数据类型之字符串(四) 判断方法 python中有一类用来判断字符串形式的方法,该类方法有两个特点:(1)方法名都是is开头(除了startswith和endswith):(2)返回值都是bool类型(True\False). 方法包括:startswith\endswith.isalnum\isalpha.isdecimal\isdigit\isnumeric.isidentifier.isprintable.isspace 1.startswith.endswith 这两个方