数字(int):

  1、int()(将字符串换为数字)

a = ""
print(type(a), a) b = int(a)
print(type(b), b) num = "a"
# 使用 int 方法时默认转换为十进制数
# 通过base来指定转换后的类型
v = int(num, base=16)
print(v)

 输出:

<class 'str'> 123
<class 'int'> 123
10

  2、bit_length() (当前数字的二进制前面的零不算)

a1 = 2  #
a2 = 3 #
v1 = a1.bit_length()
v2 = a2.bit_length()
print(v1)
print(v2)

输出

2
2

字符串(str):

  1、capitalize()

test = "aiden"
# 首字母大写
v = test.capitalize()
print(v)

输出:

Aiden

  2、casefold() 和 lower() (转化大小写)

test = "aiDen"
v1 = test.casefold()
print(v1) v2 = test.lower()
print(v2)

输出:

aiden
aiden

lower() 只针对英文的大小写;casefold() 可以转换很多未知的大小写对应关系

  3、center(); ljust(); rjust

test = "aiden"
# 设置宽度, 并将内容居中
# 20 代指总长度
# * 空白位置的填充(一个字符,包括中文),默认为空
v = test.center(20, "*")
print(v)

输出:

*******aiden********
test = "aiden"
v1 = test.ljust(20, "*")
v2 = test.rjust(20, "*")
print(v1)
print(v2)

输出:

aiden***************
***************aiden

  4、count()

test = "aidenaiden"
# 在字符串中查找子列的个数
# 2 表示从第 2 个开始;4 表示到第 3 个时结束(从零开始计数)
v1 = test.count("a")
v2 = test.count("de", 2, 4)
print(v1)
print(v2)

输出:

2
1

  5、encode()

  6、decode()

  7、endswith() 和 startwith()

test = "aidenaiden"
# 以什么开始(可设置开始和结束的参数)
v = test.startswith("ai", 0, 2 )
print(v)
# 以什么结尾(可设置开始和结束的参数)
v = test.endswith("d")
print(v)

输出:

True
False

  8、find()

# 从开始往后找,找到第一个后获取其位置(可设置开始和结束的参数)
test = "aidenaiden"
v = test.find("de",2, 4 )
print(v)

输出:

2

  9、formate() 和 formate_map()

# 格式化:将字符中的占位符替换为指定的值
test = "My name is {name},my age is {age}"
print(test)
v = test.format(name="Aiden", age=18)
print(v)

输出:

My name is {name},my age is {age}
My name is Aiden,my age is 18
# 格式化:将字符中的占位符替换为指定的值
# 可以指定位序(空则默认从零开始)
test = "My name is {0},my age is {1}"
print(test)
v = test.format("Aiden", 18)
print(v)

输出:

My name is {0},my age is {1}
My name is Aiden,my age is 18
# 传入的值为字典
test = "My name is {name},my age is {age}"
v = test.format_map({"name": "Aiden", "age": 18})
print(v)

输出:

My name is Aiden,my age is 18

  10、isalnum()

# 字符串中是否只包含数字和字母
test1 = "agfgdge123+-"
test2 = "aiden"
v1 = test1.isalnum()
v2 =test2.isalnum()
print(v1)
print(v2)

输出:

False
True

  11、expendtabs()

# 断句输出\t前的字符若不够则空格补齐(及字符串和\t长度相加为 10 遇到\n换行)
test = "name\tage\taddress\temail\naiden\t18\twenzhou\t@qq.com\naiden\t18\twenzhou\t@qq.com\n"
v = test.expandtabs(10)
print(v)

输出:

name      age       address   email
aiden 18 wenzhou @qq.com
aiden 18 wenzhou @qq.com

  12、isalpha()

# 判断字符串是否由字母组成(包括汉字)
test1 = "天fag"
test2 = "12afag"
v1 = test1.isalpha()
v2 = test2.isalpha()
print(v1)
print(v2)

输出:

True
False

  13、isdecimal(), isdigital(), isnumeric()

# 判断当前输入的是否是数字
test = ""
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1, v2, v3)

输出:

True True True

三个都能输出数字

# 判断当前输入的是否是数字
test = "123②"
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1, v2, v3)

输出:

False True True

isdigit() 可以输出符号数字(②)

# 判断当前输入的是否是数字
test = "123②二"
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1, v2, v3)

输出:

False False True

isnumeric() 可以输出中文数字

  14、isprintable()

# 输出时是否有不可见的字符
test1 = "abcd"
test2 = "abc\td"
v1 = test1.isprintable()
v2 = test2.isprintable()
print(v1, v2)

输出:

True False

  15、isspace()

# 判断是否全部为空格
test1 = "ab cd"
test2 = " "
v1 = test1.isspace()
v2 = test2.isspace()
print(v1, v2)

输出:

False True

  16、title() 和 istitle()

# istitle()判断是否为标题
# title()将每个字符串的首字母大写
test = "Return True if the string is a title-cased string, False otherwise."
v1 = test.istitle()
print(v1)
v2 = test.title()
print(v2)
v3 = v2.istitle()
print(v3)

输出:

False
Return True If The String Is A Title-Cased String, False Otherwise.
True

  17、join()(别的数据类型也可以用)

# 将字符串中的每一个元素按照指定的字符进行拼接
test = "或许爱你还能算一种天分"
print(test)
# t = " "
v = " ".join(test)
print(v)

输出:

或许爱你还能算一种天分
或 许 爱 你 还 能 算 一 种 天 分

  18、lower(), islower(); upper(), isupper()

test = "Aiden"
v1 = test.lower()
print(v1)
v1 = v1.islower()
print(v1)

输出

aiden
True
test = "Aiden"
v1 = test.upper()
print(v1)
v1 = v1.isupper()
print(v1)

输出:

AIDEN
True

  19、strip(), lstrip(), rstrip()

# 默认删除空白(空格,\t, \n)
# strip 删除两边
# lstrip 删除左边
# rstrip 删除右边
test = "\n Aiden \t"
v1 = test.strip()
v2 = test.lstrip()
v3 = test.rstrip()
print(v1)
print(v2)
print(v3)

输出:

Aiden
Aiden      Aiden

也可以删除指定字符

# 移除指定字符
# 按照有限最多匹配
test = "xAiendenx"
v1 = test.strip("x")
v2 = test.lstrip("x")
v3 = test.rstrip("enxd")
print(v1)
print(v2)
print(v3)

输出:

Aienden
Aiendenx
xAi

  20、maketrans() 和 translate()

# 将字符串按照对应关系替换
test1 = "aeiou"
test2 = ""
test = "asqwoeradurqoienuato"
# 设置对应关系
m = str.maketrans(test1, test2)
v = test.translate(m)
print(v)

输出:

1sqw42r1d5rq432n51t4

  21、partition() 和 rpartition()

# 只能将字符串按照指定的字符串分割成三份
test = "aidden"
v1 = test.partition("d")
v2 = test.rpartition("d")
print(v1)
print(v2)

输出:

('ai', 'dd', 'en')
('aid', 'd', 'en')

  22、split(), rsplit(), splitlines()

# 将字符串按照指定的字符串和指定的次数分隔
# 但不能获取指定的分割字符串
test = "adaidenda"
v1 = test.split("d", 2)
v2 = test.rsplit("d", 2)
print(v1)
print(v2)

输出:

['a', 'ai', 'enda']
['adai', 'en', 'a']
# 只根据换行符号分割
test = "ad\naiden\nda"
v1 = test.splitlines() # 默认False
v2 = test.splitlines(True)
print(v1)
print(v2)

输出:

['ad', 'aiden', 'da']
['ad\n', 'aiden\n', 'da']

  23、swapcase()

# 大小写装换
test = "Aiden"
v1 = test.swapcase()
print(v1)

输出:

AaIDEN

  24、replace()

# 将字符串中的字符替换为指定的字符
# 可指定替换的个数
test = "aidenaidenaiden"
v1 = test.replace("den", "b")
v2 = test.replace("den", "b", 2)
print(v1)
print(v2)

输出:

aibaibaib
aibaibaiden

  25、将数字转换为字符串

# 数字转换为字符串
s = 123
v = str(s)
print(v, type(v))

输出:

123 <class 'str'>

  26、列表转化成字符串(元组类似)

# 列表中只有字符串时
li = ["aiden", "nihao", "alix"]
v = "".join(li)  # join本质上使用for循环
print(v)

输出:

aidennihaoalix
# 列表中既有字符串又有数字时
# 需要自己写for循环
li = [123, "nihao", "alix"]
s = ""
for i in li:
s += str(i)
print(s)

输出:

123nihaoalix

  27、其他(别的数据类型也可以用)

test = "aiden"
# 索引
print(test[0])
# 切片
print(test[0:2]) # >=0 <2
print(test[0:-1])
# for 循环
for i in test:
print(i)

输出:

a
ai
aide
a
i
d
e
n
a
ai
ai

字符串一旦创建不可修改;一旦拼接或者修改都会重新生成字符串。

Python - 基本数据类型及其常用的方法之数字与字符串的更多相关文章

  1. Python - 基本数据类型及其常用的方法之字典和布尔值

    字典 特点:{"key1": value1, "key2":value2}  , 键值对中的值可以为任何数据类型,键不能为列表.字典(无法哈希),布尔值可以为键 ...

  2. Python - 基本数据类型及其常用的方法之元组

    元组 特点:一级元素无法被修改,且不能被增加或者删除. 基本操作: tu = (11, 22, ["aiden", 33, ("qwe", 11)], 77) ...

  3. Python - 基本数据类型及其常用的方法之列表

    列表: 特点:用 [] 括起来,切元素用逗号分隔:列表内的元素可以为任何的数据类型. 列表的基本操作: 1.修改 li = [12, 5, 6, ["Aiden", [2, 4], ...

  4. Python 数据类型,常用函数方法分类

    Python基本数据类型:(int) 字符串(str)列表(list)元组(tuple)字典(dict)布尔(bool) python中可以简单使用 类型(数据)创建或转换数据 例: #字符串转数字 ...

  5. Python学习入门基础教程(learning Python)--8.3 字典常用的方法函数介绍

    本节的主要讨论内容是有关dict字典的一些常用的方法函数的使用和范例展示. 1. clear清除字典数据 语法结构如下: dict_obj.clear() 示例代码如下: dict1 = {'web' ...

  6. Python3的基本数据类型及常用的方法

    python3的基本数据类型: 在python3当中有这么几种基本的数据类型:int(整形).str(字符串).list(列表).tuple(元组).dict(字典).bool(布尔值)等.数字整体划 ...

  7. python 基本数据类型以及内置方法

    一.数字类型 # 一.整型int # ======================================基本使用====================================== ...

  8. python学习之路-基本数据类型1 变量的概念、数字、字符串

    1 什么是数据类型? 每种编程语言都有自己的数据类型,用于标识计算机可以认识的数据,Python中主要的数据类型为字符串,整数,浮点数,列表,元祖,字典,集合七种主要的数据类型,其中以列表,字典为最主 ...

  9. python实现语音信号处理常用度量方法

    信噪比(SNR) 有用信号功率与噪声功率的比(此处功率为平均功率),也等于幅度比的平方 $$SNR(dB)=10\log_{10}\frac{\sum_{n=0}^{N-1}s^2(n)}{\sum_ ...

随机推荐

  1. IQueryable 和 IEnumerable(二)

    IQueryable 和 IEnumerable的扩展方法 一  我们从ef的DbSet<T>看起来,我们看到他继承了IQueryable<T> 和 IEnumerable&l ...

  2. mock.js使用教程

    转载自:https://blog.csdn.net/qq_42205731/article/details/81705350 cdn引入文件 :<script src="http:// ...

  3. js拼接HTML onclick传参,,页面转义符

    字符串 1 使用" .比如: ("'+key+'")例: htmlStr = htmlStr + '<span><img src="'+src ...

  4. postgresql使用pg_dump和pg_restore 实现跨服务器的数据库迁移或备份

    因为业务需求,需要将服务器上的postgre多个数据库的数据整个库得迁移到另一个postgre数据库上. 一般表较少时,会使用postgre 的copy to 和 copy from 命令就能完成表的 ...

  5. 下载和安装mongodb4.2.0+robmongo可视化工具

    一.mongodb下载安装 1.mongodb下载地址:https://www.mongodb.com/download-center/community?jmp=nav 下了很久很久,可以找其他途径 ...

  6. BigDecimal踩过的大坑

    通常Java中涉及金钱相关的计算为了保持精度,会采用BigDecimal来实现,但是BigDecimal中创建BigDecimal类对象的时候,如果使用直接new的话,必须是String类型的参数,否 ...

  7. JavaScript 对象与函数

    对象参考手册 Array Boolean Date Math Number String RegExp Global 前言 在js中什么都是对象(包括函数). 函数是用来实现具体功能的代码,用一种方式 ...

  8. Python 学习杂项

    #print("Hello World!") #name = "nihfjkds" age = 454 num1 = 1 num2 = 2 #print(nam ...

  9. Nacos v0.7.0:对接CMDB,实现基于标签的服务发现能力

    Nacos近期发布了0.7.0版本,该版本支持对接第三方CMDB获取CMDB数据.使用Selector机制来配置服务的路由类型.支持单机模式使用MySQL数据库.上线Node.js客户端,并修复了一些 ...

  10. thinkphp 应用编译

    应用编译机制作为ThinkPHP独创的功能特色,从1.0版本就延续至今,3.2版本的编译机制更加具有特色. 应用编译缓存 编译缓存的基础原理是第一次运行的时候把核心需要加载的文件去掉空白和注释后合并到 ...