1. 程式人生 > >python學習之字符串轉換

python學習之字符串轉換

bsp 左右 print pri iss 組合 spl title alpha

配置環境:python 3.6 python編輯器:pycharm

代碼如下:

#!/usr/bin/env python
#-*- coding: utf-8 -*-

def strCase():
    "字符串大小寫轉換"
    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學習之字符串轉換