python字符串学习实例

"""
字符串操作
"""
#######################capitalize字符串的首字母大写##########################
a="aliC"
a.capitalize()  #将字符传递额首字母大写但是不修改a的值
print(a.capitalize())
print(a.count("a"))
#将字符串中的所有大写字母转换成小写字母
print(a.casefold())  # Return a version of the string suitable for caseless comparisons.
print(a.center(50,"_"))  #将字符串放在50个_的中间
print(a.encode("utf-8"))  #将字符编码成byte格式
print(a.endswith("e"))  #判断一个字符串是否以e结尾
print(a.expandtabs(67))  #将字符串中的tab符号"\t"转换成空格参数为空格了的数量
print(a.find("a"))  #查找字符串中a的位置
#字符串中使用format方法的引用以及使用
print("my name is {}\nmy age is {}".format("guode",30))
print("my name is {x}\nmy age is {y}".format(y=29,x="guode"))
#format_map 方法的使用:调用字典中的关键字
print("my name is {name},my age is {age}".format_map({"name":"guode","age":34}))
print(a.index("a"))   #查询“a”在字符串中的位置
print(a.isalnum())  #判断字符串是不是一个字母和数字组成的字符串Return True if the string is an alpha-numeric string, False otherwise.
print(a.isalpha())  #判断字符串是不是纯字母Return True if the string is an alphabetic string, False otherwise
print(a.isnumeric())#Return True if the string is a numeric string, False otherwise
print(a.isdigit())  #Return True if the string is a digit string, False otherwise.
print(a.isascii())  #Return True if all characters in the string are ASCII, False otherwise.
print(a.isdecimal()) # Return True if the string is a decimal(小数,浮点数) string, False otherwise.
print(a.isidentifier())#Return True if the string is a valid Python identifier, False otherwise.如果字符串是有效的Python标识符,则返回True;否 则的话。
print(a.islower()) # Return True if the string is a lowercase string(小写字符串), False otherwise.
print(a.isupper()) #Return True if the string is an uppercase string, False otherwise.
print(a.isprintable()) #  Return True if the string is printable, False otherwise.
print("    ".isspace())  # Return True if the string is a whitespace(空格) string, False otherwise
print(a.istitle())   #Return True if the string is a title-cased string, False otherwise.
print(".".join(["www","baidu","com"]))
print("|".join(["www","baidu","com"]))
print(a.ljust(20,"@"))
#str.maketrans主要是建立一个映射表
a="abacaba"
name=str.maketrans("abc","123")
print(a.translate(name))
##################################
print(a.partition("bc"))  #Partition the string into three parts using the given separator.
print(a)
print(a.replace("a","A"))
print(a.replace("a","A"))
print(a.rfind("a",0,len(a)))  #查找最后一个a的索引
print(a.rindex("a",0,len(a)))
print(a.rjust(50,"#"))
print(a.rsplit("a"))  #split 分隔,分开
print(a.split("a"))
print(a.rstrip())
print(" nihaoa lakdljfals   \n ".strip())  #去除字符串开头和末尾的的空格,或者\n 等等
print("www\nbaidu\ncom".splitlines())  #Return a list of the lines in the string, breaking at line boundaries.返回字符串中的线列表,在行边界处断开。
print(a.swapcase()) #所有小写转换成大写
print("we are chinese".title())
print(a.upper())
print("4".zfill(6))  # Pad a numeric string with zeros on the left, to fill a field of the given width.  The string is never truncated.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值