Python笔记2 :字符串

 字符串格式化输出:

weight=75
print("*%d*"%(weight))  
print("*%5d*"%(weight))    #至少五个字符,不足从前面补空格
print("*%2d*"%(weight))   #至少2个字符

print("*%-5d*"%(weight))   #至少五个字符,不足从后面补空格
print("*%-2d*"%(weight))

print("*%.5d*"%(weight))   #至少五个字符,不足从前面补零
print("*%.2d*"%(weight))   #至少俩字符
#out
*75*
*   75*
*725*

*75   *
*725*

*00075*
*725*


weight=7.25
print("*%f*"%(weight))

print("*%5f*"%(weight))      #至少五个字符,满足条件
print("*%9f*"%(weight))      #9代表至少要有9个字符,不足从前面补空格

print("*%-5f*"%(weight))         
print("*%-9f*"%(weight))     #-9代表至少要有9个字符,不足从后面补空格

print("*%.5f*"%(weight))     #.5代表小数点后5位
print("*%.3f*"%(weight))
#out
*7.250000*

*7.250000*
* 7.250000*

*7.250000*
*7.250000 *

*7.25000*
*7.250*

字符串的内置函数

          特殊:

          字符串转为列表的函数:split()和splitlines

          列表、字符串、元组、字典 转 字符串:'sep'.join(seq)

##############字符串 转 字符串############## strip([chars])
'''
原型:
功能:在字符串上执行lstrip和rstrip截取左右两侧制定字符,
参数:
'''
str27 = "*****sunck is a good man**"
str28 = str27.strip("*")
print(str27)
print(str28)

##############字符串 转 列表############## split(str="", num=string.count(str))
'''
原型:
功能:                                             
参数:
'''
str29 = "sunck#is#a#good#man"
wordList = str29.split("#")
print(wordList)
print(type(wordList))

##############字符串 转 列表 ################ splitlines()
'''
原型:
功能:按照行('\r','\r\n','\n')进行切割。          
                                                   
参数:
'''
str30 = """sunck
is 
a
good 
man"""
list1 = str30.splitlines()
print(list1)

###############列表、字符串、元组、字典 转 字符串################'sep'.join(seq)
"""
语法 'sep'.join(seq)
参数说明
sep: 分隔符,可以为空
seq: 要链接的元素序列,字符串、元组、字典
以sep作为分隔符,将seq所有的元素合并成一个新的字符串
返回值: 返回一个以分隔符sep连接各个元素后生成的字符串      
"""
seq1 = ['hello','good','boy','doiido']
print (' '.join(seq1))
#hello good boy doiido
print (':'.join(seq1))
#hello:good:boy:doiido

# #########字符串 返回 字符串#################replace(old, new [, max])
'''
原型:
功能:将字符串中的old替换成new,如果max指定,则替换不超过max次
参数:                                          
'''
str33 = "sunck is a good man, sunck is a nice man, sunck is a cool man"
str34 = str33.replace("sunck", "kaige")
print(str34)

# startswith(str[, beg=0,end=len(string)])
'''
原型:
功能:检查字符串是否以str开头,是则返回True, 否则返回False。如果指定了beg和gend,则在指定范围内检查
参数:
'''
str40 = "sunck is a good man"
print(str40.startswith("good"))
# endswith(suffix[, beg=0, end=len(string)])
'''
原型:
功能:检查字符串是否以suffix结尾,是则返回True, 否则返回False。如果指定了beg和gend,则在指定范围内检查
参数:
'''

# encode(encoding='UTF-8',errors='strict')
'''
原型:
功能:以encoding指定的编码格式进行编码,如果出错默认报一个ValueError异常,除非errors指定的是igonre或者replace
参数:
'''
str41 = "凯哥是一个好男人"
str42 = str41.encode()
str43 = str41.encode("GBK") #gb2312
print(str41)
print(str42)
print(str43)

# bytes.decode(encoding="utf-8", errors="strict")
'''
原型:
功能:解码
参数:
'''
print(str42.decode("utf-8"))
print(str43.decode("GBK"))

# ord()
'''
原型:
功能:获取字符的整数表示ASCII码
参数:
'''
print(ord("A"))
print(ord("凯"))
# chr()
'''
原型:
功能:把数字编码转为对应的ASCII字符
参数:
'''
print(chr(65))
print(chr(20975))
# str()
'''
原型:
功能:转成字符串
参数:
'''
print(str(123))
print(str(123.1))
print(type(str(True)))
#############################eval(*args, **kwargs)
'''
原型:eval(*args, **kwargs)
功能:将字符串当成有效的表达式来求值并返回计算结果
参数:
'''
num1 = eval("123")
print(num1)
print(type(num1))
print(eval("+123"))
print(eval("-123"))
print(eval("12+3"))
print(eval("12-3"))
# print(eval("a123"))
# print(eval("12a3"))
abc = "123"
print(eval("abc"))

################################ lower()
'''
原型:
功能:转换字符串中所有的大写字母为小写
参数:
'''
str1 = "Sunck Is A Good maN"
str2 = str1.lower()
print(str1, str2)
############################### upper()
'''
原型:
功能:转换字符串中所有的小写字母为大写
参数:
'''
############################### swapcase()
'''
原型:
功能:将字符串中大写转为小写,小写转为大写
参数:
'''
str3 = "Sunck Is A Good maN"
str4 = str3.swapcase()
print(str3, str4)

############################### capitalize()
'''
原型:
功能:将字符串中第一个字符转为大写,其余转为小写
参数:
'''
str5 = "sunck Is A Good maN"
str6 = str5.capitalize()
print(str5, str6)
################################# title()
'''
原型:
功能:得到“标题化”的字符串,每个单词的首字符大写,其余小写
参数:
'''
str7 = "sunck Is A Good maN"
str8 = str7.title()
print(str7, str8)

############################## center(width[, fillchar])
'''
原型:
功能:返回一个指定宽度width的居中字符串,fillchar为填充字符,默认为空格
参数:
'''
str9 = "good"
str10 = str9.center(20, "#")
print("*"+str10+"*")

# ljust(width[, fillchar])
'''
原型:
功能:返回一个指定宽度width的左对齐字符串,fillchar为填充字符,默认为空格
参数:
'''
str11 = "good"
str12 = str11.ljust(20, "#")
print("*"+str12+"*")
# rjust(width,[, fillchar])
'''
原型:
功能:返回一个指定宽度width的右对齐字符串,fillchar为填充字符,默认为空格
参数:
'''
# zfill (width)
'''
原型:
功能:返回指定宽度width的右对齐字符串,填充0
参数:
'''
str13 = "good"
str14 = str13.zfill(20)
print("*"+str14+"*")


# count(str[, beg= 0,end=len(string)])
'''
原型:
功能:返回str在string里面出现的次数,如果beg或者end指定则返回指定范围内str出现的次数
参数:
'''
str15 = "sunck"
str16 = "sunck is a good man! sunck is a nice man"
print(str16.count(str15,0,20))
# find(str[, beg=0 end=len(string)])
'''
原型:
功能:检测str是否包含在string中,如果指定beg和end,则检测指定范围内是否包含。如果包含返回第一个开始的索引值,否则返回-1
参数:
'''
str17 = "sunck"
str18 = "cool"
str19 = "aaasunck is a good man! sunck is a nice man"
res = str19.find(str18)
print(res)

# lstrip()
'''
原型:
功能:截掉字符串左边指定的字符,默认为空格
参数:
'''
str23 = "    sunck is a good man"
str24 = str23.lstrip()
print(str23)
print(str24)
str25 = "*****sunck is a good man"
str26 = str25.lstrip("*")
print(str25)
print(str26)
# rstrip()
'''
原型:
功能:截掉字符串右边指定的字符,默认为空格
参数:
'''

# maketrans()
'''
原型:
功能:创建字符映射的转换表,对于接受两个参数的,第一个是字符串,表示要转换的字符,第二个也是字符串表示转换的目标
参数:
'''
t = str.maketrans("ag", "12")
# translate(table)
'''
原型:
功能:根据str给出的表转换字符串
参数:
'''
str35 = "sunck is a good manag"
print(str35.translate(t))

# isalpha()
'''
原型:
功能:如果字符串至少有一个字符并且所有的字符都是字母返回True,否则返回假
参数:
'''
print("".isalpha())
print("abc".isalpha())
print("abc12".isalpha())

# isalnum()
'''
原型:
功能:如果字符串至少有一个字符并且所有的字符都是字母或数字返回True,否则返回假
参数:
'''
print("".isalnum())
print("abc".isalnum())
print("abc12".isalnum())

# isupper()
'''
原型:
功能:如果字符串至少有一个字符并且所有的字母都是大写字母返回True,否则返回假
参数:
'''
print("sunck".isupper())
print("SUNCK".isupper())
print("SUNCK#$%".isupper())
print("SUNCK123".isupper())
print("".isupper())
# islower()
'''
原型:
功能:如果字符串至少有一个字符并且所有的字母都是小写字母返回True,否则返回假
参数:
'''
# istitle()
'''
原型:
功能:如果字符串是标题化的返回True,否则返回False
参数:
'''
# isdigit()
'''
原型:
功能:如果字符串只包含数字返回True,否则返回False
参数:
'''
print("".isdigit())
print("123".isdigit())
print("abc123".isdigit())
# isnumeric()
'''
原型:
功能:如果字符串只包含数字返回True,否则返回False
参数:
'''
# isdecimal()
'''
原型:
功能:检测字符串是否只包含十进制数字
参数:
'''
# isspace()
'''
原型:
功能:如果字符串中只含有空格则返回True,否则返回False
参数:
'''
print("".isspace())
print("   ".isspace())
print(" a".isspace())
print("\t".isspace())
print("\n".isspace())

print("--------------------")

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值