字符

# 拼接方法
# s="hello"+" world"+" world"
# 占内存 因为"hello"、" world"、" world"各在内存占一个空间,"hello"+" world"的结果也占一个空间,才到结果
# print(s)

# join
# print("+".join(["i","am","world"])) # 这个效率高

# 拆分
s="hello word".split(" ") # 用空格分割
print(s)
s="hello word".split("e") # 用e分割
print(s)
s="hello word".split("l",1) # 用e分割
print(s)

# 查找字符
print("hello world".find("l"))
print("hello world".rfind("l")) # 从右开始找
print("hello world".find("l",3)) # 从第3个开始找

# print("hello world".index("x"))
# print("hello world".find("x"))

# 替换(完全匹配)
print("hello world".replace("world","python"))

# 替换(模糊匹配):用正则表达式匹配

print("hello world".center(50,"*")) # 一共50个空位,用*填补前后
print("hello world".ljust(50,"*")) # 一共50个空位,用*填补前后

print("hello %s"%"sb")
print("hello %s,%s"%("sb","egon"))
print("hello %s,%d"%("sb",23)) # %d是整型数值占位 %d是字符串 %f浮点型
print("hello %s,%s"%("sb",23)) # %s也可以输出23
print("hello %s,%.4f"%("sb",23.567894)) # %.4f只保留4位小数

# "hello world".format()

print("hello {0}, his age is {1}".format("alex",23))
print("hello {name}, his age is {age}".format(name="alex",age=23))

print("1".isdecimal()) #是否十进制数字
print("1".isdigit()) #是否数字 这个最常用
print("1".isnumeric()) #是否数字,一也是数字,其他不算数字

print("一".isdecimal()) #是否十进制数字
print("1".isdigit()) #是否数字 这个最常用
print("1".isnumeric()) #是否数字,一也是数字,其他不算数字

print("hello world".capitalize()) # 第一个大写
print("hello world".title()) # 单词前都大写
print("hello world".title()) # 单词前都大写

-----------------------------------------------------
# coding:gbk

s="中国"
print(s)

字符
整型
浮点型 0.0000123 = 1.23e-5
字符型 s="" s=str() l=[1,2,3] l=list()
转义符号
(1)s='let\'s go'
(2)print(r"\sfsefe") # r表示里面全部符号都是普通符号
s1="hello world"
print(s1[1:4]) #取ell
print(s1[1:4:2]) #2是步长
print(s1[-1]) #取最后一个
print(s1[:]) #取全部
print(s1[-3:-1]) #取rl


长整型(只在python 2.x有,在python 3已经没有了)
i=100000000000000000000000
print(type(i))
布尔类型 true 1 false 0


可变数据类型 列表 字典
不可变数据类型 :整型 元组 字符串 i=1 i=2 2是重新在另一个区域创建 1还在原来的区域,没有变


#
# 对象:通过一个点调用一个方法 方法()
#
# a=1
# b=a
# 变量:
# 1、不能以数字开头或特殊字符 % 2a
# 2、不能以关键字 if
# 3、
# 缩进,习惯空4个格
# if 2>1:
# print ("234")
#
# # 运算符
# 算术运算符 + - * /
# 赋值运算符 = += -= *= /= i=3 i+=1(即i=i+1)
# 比较运算符 == > < >= <= != (结果都是true或false)
# 逻辑运算符 and or not (or其中一个对就是对)
# print(False and False)----False
# 返回值取决于谁决定了结果
# print(0 and 2) 由于0决定了结果肯定是0,所以结果是0
# print(1 and 2) 由于1决定不了结果,所以结果是2
# 关系运算符 is in not in
# print (2 in [1,2,3])
# 位运算符 (用太少)


strip() :把字符串开头,结尾的(空格和\n)去掉。匹配往往就需要用这个语句,否则匹配不上
s=" hello\nworld\n".strip()
s1=" * hello\nworld\n".strip("*") # 需要*在开头或结尾
print(s)
print(s1)

----------------------------------------------------------------
i=100000000000000000000000
print(type(i))
s=" hello\nworld\n".strip()
s1="* hello\nworld\n".strip("*")
print(s)
print(s1)





转载于:https://www.cnblogs.com/jensenxie/p/8457732.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值