数字(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. 6_再次开中断STI的正确姿势

    1 直接开启sti --蓝屏 2 配置环境 正确开启sti 中断 kpcr -- 很多重要线程切换的数据.结构 进入内核的时候 fs 不再是teb/tib: 是kpcr. 同时观察 kifastcal ...

  2. c结构体指针使用

    #include <stdio.h> #include<stdlib.h> #include<string.h> typedef struct _Date { un ...

  3. lvs负载均衡连接

    http://blog.csdn.net/zwz1984/article/details/45194377 http://blog.csdn.net/zwz1984/article/details/4 ...

  4. yii2 返回json和文件下载

    JSON 調用的控制器返回 json格式的數據即可,對json裡面的數據沒有要求 如在控制器中添加一個方法: public function actionRemoveImage($id){ Yii:: ...

  5. R语言 数据重塑

    R语言数据重塑 R语言中的数据重塑是关于改变数据被组织成行和列的方式. 大多数时间R语言中的数据处理是通过将输入数据作为数据帧来完成的. 很容易从数据帧的行和列中提取数据,但是在某些情况下,我们需要的 ...

  6. 概率期望——cf round362 div1

    给定n个数,求i的位置的期望 那么反向考虑,j!=i排在i前面的概率是0.5,那么对i的位置的期望贡献就是1*0.5 这题就是拓展应用一下 #include<bits/stdc++.h> ...

  7. [CTSC 2012]熟悉的文章

    二分+单调队列优化dp+后缀自动机 //CTSC2012 熟悉的文章 #include <bits/stdc++.h> using namespace std; const int max ...

  8. hdu多校第一场1003 (hdu6580)Milk 背包

    题意: 有一个n*m的矩阵,左右可以随便走,但只能在每一行的中点往下走,每走一格花费时间1. 现在这个矩阵里放了k瓶牛奶,第i个牛奶喝下去需要ti时间 起点是(1,1) 对于每个i∈[1,k],问喝掉 ...

  9. 09_springmvc图片上传

    一.上传图片 1.需求 在修改商品页面,添加上传商品图片的功能 2.springmvc中对多部件类型解析 在页面form中提交enctype="multipart/form-data&quo ...

  10. Python系统(os)相关操作

    文件操作 python中常用于文件处理的模块有os,shutil等. 1 创建文件 文件的创建可以使用open()函数,如下创建一个test_file.txt的文件: >>> wit ...