配置環境:python 3.6 python編輯器:pycharm
**如下:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
defstrcase():
"字串大小寫轉換
"print("
演示字串大小寫轉換")
print("
演示字串s賦值為:' this is a python '")
s = '
this is a python
'print("
大寫轉換成小寫:\ts.lower() \t= %s
"%(s.lower()))
print("
小寫轉換成大寫:\ts.upper() \t= %s
"%(s.upper()))
print("
大小寫轉換:\t\ts.swapcase() \t= %s
"%(s.swapcase()))
print("
首字母大寫:\t\ts.title() \t= %s
"%(s.title()))
print('\n'
)def
strfind():
"字串搜尋、替換
"print("
演示字串搜尋、替換等")
print("
演示字串s賦值為:' this is a python '")
s = '
this is a python
'print("
字串搜尋:\t\ts.find('is') \t= %s
"%(s.find('is'
)))
print("
字串統計:\t\ts.count('s') \t= %s
"%(s.count('s'
)))
print("
字串替換:\t\ts.replace('is','is') = %s
"%(s.replace('
is','is'
)))
print("
去左右空格:\t\ts.strip() \t=#%s#
"%(s.strip()))
print("
去左邊空格:\t\ts.lstrip() \t=#%s#
"%(s.lstrip()))
print("
去右邊空格:\t\ts.rstrip() \t=#%s#
"%(s.rstrip()))
print('\n'
)def
strsplit():
"字串分割、組合
"print("
演示字串分割、組合")
print("
演示字串s賦值為:' this is a python '")
s = '
this is a python
'print("
字串分割:\t\ts.split() \t= %s
"%(s.split()))
print("
字串組合1: '#'.join(['this','is','a','python']) \t= %s
"%('
#'.join(['
this
','is
','a
','python
'])))
print("
字串組合2: '$'.join(['this','is','a','python']) \t= %s
"%('
$'.join(['
this
','is
','a
','python
'])))
print("
字串組合3: ' '.join(['this','is','a','python']) \t= %s
"%('
'.join(['
this
','is
','a
','python
'])))
print('\n'
)def
strtest():
"字串測試
"print("
演示字串測試")
print("
演示字串s1賦值為:'abcd'")
s1 = '
abcd
'print("
測試s.isalpha() = %s
"%(s1.isalpha()))
print("
測試s.isdigit() = %s
"%(s1.isdigit()))
print("
測試s.isspace() = %s
"%(s1.isspace()))
print("
測試s.islower() = %s
"%(s1.islower()))
print("
測試s.isupper() = %s
"%(s1.isupper()))
print("
測試s.istitle() = %s
"%(s1.istitle()))
if__name__ == '
__main__':
strcase()
strfind()
strsplit()
strtest()
Python學習之字串
字串或串 string 是由數字 字母 下劃線組成的一串字元。一般記為 s a1a2 an n 0 它是程式語言中表示文字的資料型別。python的字串列表有2種取值順序 如果你的實要取得一段子串的話,可以用到變數 頭下標 尾下標 就可以擷取相應的字串,其中下標是從0開始算起,可以是正數或負數,下標...
python學習之字串
1 賦值 msg studying python now msg1 xu te t為4個空格 msg3 aaa qq.com msg4 d1 2 字串常用方法 print msg.capitalize 首字母大寫 print msg.center 40,按40個字元寬度居中顯示字串,前後用 填充 p...
PYTHON 學習之字串變數
利用百分號格式化 name zhangsan age 25 price 4500.225 print my name is s name print i am d age years old print my price is f price 保留指定位數小數 四捨五入 print my price...