Python - 字符串
生活随笔
收集整理的這篇文章主要介紹了
Python - 字符串
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#---------------------------------------------------------------------------------------
# 字符串連接 : 方法1 '+'
str0 = 'hello'
str1 = "python"
print(str0 + str1) #'hellopython'# 字符串連接 : 方法2 連接對象.join() : 在每個單詞之間用連接對象連接起來
lst = ['www','google','com']
lst_join = ".".join(lst)
print(lst_join) #'www.google.com'#---------------------------------------------------------------------------------------
# 格式化字符串1: 占位符'{}'
name = 'Tom'
age = 20
addr = "上海市"
info = '{0},{1},{2}'.format(name, age, addr)
print(info) #'Tom,20,上海市'#---------------------------------------------------------------------------------------
# 格式化字符串2: 占位符'{}'
print("{name},{age},{addr}".format(name = "Tom", age = 22, addr = "上海市")) #'Tom, 22, 上海市'#---------------------------------------------------------------------------------------
# 格式化字符串3: %
num = 100
str_tmp = "%d years" % num
print(str_tmp) #'100 years'
lang = "python"
print("I love %(program)s" % {"program" : lang}) #"I love python"#---------------------------------------------------------------------------------------
# len() : 獲取字符串長度
str1 = "hello"
print(len(str1)) #5#---------------------------------------------------------------------------------------
# 字符串.isalpha() : 判斷字符串對象是否全部為字母, True(全部為字符), False(含非字母)
str2 = "world2"
str3 = 'hehe he'
print('str1 : ' + str(str1.isalpha())) #True
print('str2 : ' + str(str2.isalpha())) #False
print('str3 : ' + str(str3.isalpha())) #False#--------------------------------------------------
# str.split() : 分割字符串
str4 = "www.itdiffer.com"
for str_tmp in str4.split("."):print(str_tmp) #'www' 'itdiffer' 'com'#--------------------------------------------------
# str.strip() : 去掉str左右兩邊的空格
str5 = " he ha ho "
print('去掉前,str5:' + str5 + ':長度' + str(len(str5)))
str5 = str5.strip();
print(str5)
print('去掉后,str5:' + str5 + ':長度' + str(len(str5)))#--------------------------------------------------
# str.lstrip() : 去掉str左邊的空格
str5 = " hehe"
print(str5.lstrip())#--------------------------------------------------
# str.rstrip() : 去掉str右邊的空格
str6 = "hehe "
print(str6.rstrip())#--------------------------------------------------
# str.upper() : 將小寫字母,全部轉換為大寫
str7 = "hello python"
print(str7.upper())#--------------------------------------------------
# str.lower() : 將大寫全部轉換為小寫
str8 = "HELLO PYTHON"
print(str8.lower())#--------------------------------------------------
# str.isupper() : 判斷當前字符串是否全部為大寫
str9 = "hello"
str10 = "HELLO"
print(str(str9.isupper())) #False
print(str(str10.isupper())) #True#--------------------------------------------------
# str.islower() : 判斷當前字符串是否全部為小寫
str11 = "Hello"
str12 = "python"
print(str(str11.islower())) #False
print(str(str12.islower())) #True#--------------------------------------------------
# str.title() : 將字符串中每個單詞的首字母轉換為大寫(如果字符串全部為大寫,則轉化結果為:每個單詞的首字母大寫其他字母小寫)
str13 = "hello Pyhton"
str14 = "hello python"
str15 = "HELLO PYTHON"
str13_title = str13.title() #'Hello Python'
str14_title = str14.title() #'Hello Python'
str15_title = str15.title() #'Hello Python'
print('str13:{0}'.format(str13))
print('str14:{0}'.format(str14))
print('str15:{0}'.format(str15))
print('str13_title:' + str13_title) #'str13_title:Hello Pyhton'
print('str14_title:' + str14_title) #'str14_title:Hello Pyhton'
print('str15_title:' + str15_title) #'str15_title:Hello Pyhton'
print(str(str13_title.istitle())) #True
print(str(str14_title.istitle())) #True
print(str(str15_title.istitle())) #True#--------------------------------------------------
# str.istitle() : 判斷每個單詞的首字母是否為大寫
str16 = "hEllo"
str17 = "World"
str18 = "Hello,World"
str19 = "Hello,world"
print(str(str16.istitle())) #False
print(str(str17.istitle())) #True
print(str(str18.istitle())) #True
print(str(str19.istitle())) #False#--------------------------------------------------
# str.capitalize() : 將字符串的第一個字母變為大寫
str20 = "hello python"
str20_capitalize = str20.capitalize();
print(str(str20_capitalize)) #'Hello pythn'
?
轉載于:https://www.cnblogs.com/zuiwohongchen/p/6668891.html
總結
以上是生活随笔為你收集整理的Python - 字符串的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【page-monitor 前端自动化
- 下一篇: python简易木马(一)