Python学习笔记 --- 字符串(五)

字符串

#字符串

a="Hello World!"
b="Rub00y"
 
#访问字符串中的值
print("a[0]: ",a[0])
print("b[1:5]",b[1:5])
 
-------------------------- 运行结果 --------------------------

a[0]: H
b[1:5] ub00

------------------------------------------------------------------

#python字符串更新
print("字符串更新: ",a[:6]+b+"!")
#python转移字符, (在行尾时) 续行符
-------------------------- 运行结果 --------------------------

字符串更新:  Hello Rub00y!

------------------------------------------------------------------

#字符串可以使用 + * 等运算符

a="Hello World!"
b="Rub00y"

print("a+b:",a+" "+b)
print("a*2",a*2)
if("H" in a):
    print("H在a中")
else:
    print("H不在a中")
-------------------------- 运行结果 ------------------------

a*2 Hello World!Hello World!
H在a中

------------------------------------------------------------------

Python中字符串格式化

1. %s 格式化字符串;
2. %c 格式化字符及其ascll码;
3. %d 格式化整数;
4. %u 格式化无符号整数;
5. %o 格式化无复还八进制数
6. %x 格式化无符号十六进制数;%X(大写);
8. %f 格式化浮点数字;
9. %e 用科学计数法格式化浮点数;(%g是%f和%e 的简写;)
例子:
 
print("我叫 %s 今年 %d 岁"%("小明",10))
print("浮点数 %f 科学计数 %e"%(0.264,1000000000000))
print("科学计数浮点数 %g"%0.00000000001)
...
-------------------------- 运行结果 ------------------------

我叫 小明 今年 10 岁

浮点数 0.264000 科学计数 1.000000e+12

科学计数浮点数 1e-11

...

------------------------------------------------------------------

#python 三引号 允许一个字符串跨多行,字符串中可以包含换行符、制表符以及其他的特殊符号

para_str = """这是一个多行字符串的实例
多行字符串可以使用制表符
TAB (   )。
也可以使用换行符 [   ]。
"""

print( para_str)
 
-------------------------- 运行结果 --------------------------

这是一个多行字符串的实例
多行字符串可以使用制表符
TAB ( )。
也可以使用换行符 [
]。

------------------------------------------------------------------

拓展:

字符串函数
 
#capitalize()将字符串第一个字符转换为大写
 
print(str.capitalize("ajhak")) 
-------------------------- 运行结果 --------------------------
Ajhak
------------------------------------------------------------------
#center(width,fillchar) 返回指定的宽度width。fillchar填充的字符,默认为空格
 
a="ahsjfghaj"
print(a.center(50,"-"))
-------------------------- 运行结果 --------------------------
--------------------ahsjfghaj---------------------
------------------------------------------------------------------
 
#bytes.decode() 语法:bytes.decode(encodeing="utf-8",errors="strict")
 
str="基础学习"
str_utf8=str.encode("utf-8")
print("utf-8: ",str_utf8.decode("utf-8","strict"))
-------------------------- 运行结果 --------------------------
utf-8:  基础学习
------------------------------------------------------------------
 
#count(str,beg=0,end=len(string)) 返回str在string里面出现的次数,如果beg或者end指定则返回指定范围内str出现的次数
ocunt="dahdsssssdd"
print(ocunt.count("s",0,10))
 
-------------------------- 运行结果 --------------------------
5
------------------------------------------------------------------
 
#endswith(suffix,beg=0,end=len(string))检查字符串是否以obj结束,如果beg或指定则检查指定的范围内是否以obj结束,如果是返回true,否则false
#简单理解:如果字符串含有指定的后缀返回true,否则返回为false
 
endswith="asdhnfkasjdhfkahjs"
print(endswith.endswith("ajs"))
 
-------------------------- 运行结果 --------------------------
False
------------------------------------------------------------------
#expandtabs(tabsize=8)把字符串string中的tab符号转换为空格,tab符号默认的空格数是8
expandtabs="sjaskjdhf asdf"
print(expandtabs.expandtabs()) #默认8个空格
print(expandtabs.expandtabs(tabsize=50)) #指定了50个
-------------------------- 运行结果 --------------------------

sjaskjdhf        asdf
sjaskjdhf                                                   asdf

------------------------------------------------------------------

#find(str,beg=0,end=len(string))包含子字符串返回开始的索引值,否则返回-1。
finds="ashjahsxxas"
print(finds.find("sxx",0,10))
print(finds.find("sxx",0,5))
 
-------------------------- 运行结果 --------------------------

6
-1

------------------------------------------------------------------

#index(str,beg=0,end=len(string))同find()没有检测到的就报异常
indexs="jahsdfkjashd"
print(indexs.index("h",0,11))
-------------------------- 运行结果 --------------------------

2

------------------------------------------------------------------

 
#isalnum()如果字符串至少有一个字符并且所有的字符都是字母或数字则返回为true,否则返回False
print("asfas".isalnum())  --------------- True
 
#isalpha()如果字符串至少有一个字符并且所有字符都是字母则返回为true,否则返回flase
print("asfas121".isalpha())  --------------- False
 
#isdigit()如果字符串中只包含数字返回true,否则返回flase
#islower()字符串中都是小写返回true,否则为flase
#ispace()只包含空格返回true,否则返回flase
#istitle()如果字符串标题化的返回true,否则flase
#isupper()字符串都为大写返回true,否则返回为flase

#jion(seq)指定字符串为分隔符,将seq中所有的元素(的字符串表示)合并为一个新的字符串
s1="-"
s2=""
seq=("s","t","u","d","y")
print(s1.join(seq))
print(s2.join(seq))
-------------------------- 运行结果 --------------------------

s-t-u-d-y
s t u d y

------------------------------------------------------------------
#len(string)返回字符长度
print(len("asasdfkj"))
-------------------------- 运行结果 --------------------------

8

------------------------------------------------------------------
 
#ljust(width,fillchar) 返回一个原字符串对齐。并使用fillshar填充至长度width的新字符串,fillchar默认为空格
print("sda".ljust(50,"^"))
-------------------------- 运行结果 --------------------------

sda^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

------------------------------------------------------------------
#lstrip()返回截掉字符串左边的空格或指定字符后生成的新字符
 
lstrips="    This is string example......wow!!!"
print(lstrips.lstrip())
-------------------------- 运行结果 --------------------------

This is string example......wow!!!
------------------------------------------------------------------

lstripss="----------This is string example......wow!!!----------"
print(lstripss.lstrip("-"))
-------------------------- 运行结果 --------------------------
This is string example......wow!!!----------
------------------------------------------------------------------
 
#maketrans() 语法:str.maketrans(intab,outtab)返回字符串转换后生成的新字符串
 
#max(str)返回字符串str中最大的字母
print(max("afafadsf"))  -------------------- s
 
#min(str)返回字符串str中最小的字母
print(min("asghdfgaghfasd")) ---------------- a
 
#replace(old,new,max) 把字符串中的str1替换成str2,如果指定第三个参数max。则替换不超过max次
print("ahjgfahjabhj".replace("hj","yu",2)) ----------- ayugfayuasdhj
 
#rfind()返回字符串最后一次出现的位置,如果没有匹配项则返回-1
rfinds01="This is really a string example...wow!!!"
rfinds02="is"
print(rfinds01.rfind(rfinds02))
-------------------------- 运行结果 --------------------------
5
------------------------------------------------------------------
 
#rstrip()删除字符串末尾的空格
print("ksdsdfasd ".rstrip()) ------------- ksdsdfasd
 
#split()通过指定分隔符对字符串进行切片,如果第二个参数num指定值,则分割为num+1个子字符串
 
url = "http://www.baidu.com/python/image/123456.jpg"
print(url.split(".")) 
print(url.split("/")[5:6])

-------------------------- 运行结果 --------------------------

['http://www', 'baidu', 'com/python/image/123456', 'jpg']
['123456.jpg']
------------------------------------------------------------------
#strip()用于移除字符串头尾指定的字符(默认为空格)或字符序列;注意:该方法只能删除头尾的字符串,不能删除中间的字符串
 
print(" ' ' ' 'asdfhajsdg' ' ' ' ".strip(" ' ")) ----------- asdfhajsdg
 
#swapcase()将字符串中小写转为大写,大写转小写
print("sdfaASDFjd".swapcase()) ------------- SDFAasdfJD

 
原文地址:https://www.cnblogs.com/liaolei123/p/13138543.html