Python入門教程2. 字符串基本操作【運(yùn)算、格式化輸出、常用函數(shù)】 原創(chuàng)
前面簡單介紹了Python基本運(yùn)算,這里再來簡單講述一下Python字符串相關(guān)操作
1. 字符串表示方法
>>> "m.fzitv.net" #字符串使用單引號(')或雙引號(")表示 'm.fzitv.net' >>> 'm.fzitv.net' 'm.fzitv.net' >>> "www."+"jb51"+".net" #字符串可以用“+”號連接 'm.fzitv.net' >>> "#"*10 #字符串可以使用“*”來代表重復(fù)次數(shù) '##########' >>> "What's your name?" #單引號中可以直接使用雙引號,同理雙引號中也可以直接使用單引號 "What's your name?" >>> path = r"C:\newfile" #此處r開頭表示原始字符串,里面放置的內(nèi)容都是原樣輸出 >>> print(path) C:\newfile
2. 字符串運(yùn)算
>>> str1 = "python test" >>> "test" in str1 #這里in用來判斷元素是否在序列中 True >>> len(str1) #這里len()函數(shù)求字符串長度 11 >>> max(str1) 'y' >>> min(str1) ' '
3. 字符串格式化輸出(這里重點(diǎn)講format函數(shù))
>>> "I like %s" % "python" #使用%進(jìn)行格式化輸出的經(jīng)典表示方式 'I like python' >>> dir(str) #列出字符串所有屬性與方法 ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
① format(*args,**kwargs) 采用*args賦值
>>> str = "I like {1} and {2}" #這里{1}表示占位符(注意:這里得從{0}開始)
>>> str.format("python","PHP")
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
str.format("python","PHP")
IndexError: tuple index out of range
>>> str = "I like {0} and {1}"
>>> str.format("python","PHP")
'I like python and PHP'
>>> "I like {1} and {0}".format("python","PHP")
'I like PHP and python'
>>> "I like {0:20} and {1:>20}".format("python","PHP")#{0:20}表示第一個位置占據(jù)20個字符,并且左對齊。{1:>20}表示第二個位置占據(jù)20個字符,且右對齊
'I like python and PHP'
>>> "I like {0:.2} and {1:^10.2}".format("python","PHP")#{0:.2}表示第一個位置截取2個字符,左對齊。{1:^10.2}表示第二個位置占據(jù)10個字符,且截取2個字符,^表示居中
'I like py and PH '
>>> "age: {0:4d} height: {1:6.2f}".format("32","178.55") #這里應(yīng)該是數(shù)字,不能用引號,否則會被當(dāng)作字符串而報(bào)錯!
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
"age: {0:4d} height: {1:6.2f}".format("32","178.55")
ValueError: Unknown format code 'd' for object of type 'str'
>>> "age: {0:4d} height: {1:8.2f}".format(32,178.5523154) #這里4d表示長度為4個字符的整數(shù),右對齊。8.2f表示長度為8,保留2位小數(shù)的浮點(diǎn)數(shù),右對齊。
'age: 32 height: 178.55'
② format(*args,**kwargs) 采用**kwargs賦值
>>> "I like {str1} and {str2}".format(str1 = "python",str2 ="PHP")
'I like python and PHP'
>>> data = {"str1":"PHP","str2":"Python"}
>>> "I like {str1} and {str2}".format(**data)
'I like PHP and Python'
小結(jié):對齊方式為:
| < | 左對齊 |
| > | 右對齊 |
| ^ | 居中對齊 |
4. 字符串函數(shù)
>>> # isalpha()判斷字符串是否全部為字母
>>> str1 = "I like python" #這里str1里面有空格
>>> str1.isalpha()
False
>>> str2 = "pythonDemo" #這里為全部是字母
>>> str2.isalpha()
True
>>> # split()分割字符串
>>> smp = "I like Python"
>>> smp.split(" ")
['I', 'like', 'Python']
>>> # strip()去除字符串兩端空格 ,類似的,lstrip去除左側(cè)空格,rstrip去除右側(cè)空格
>>> strDemo = " python demo "
>>> strDemo.strip() #類似于php中的trim()函數(shù)
'python demo'
>>> "****python**".strip("*") #strip()函數(shù)還可刪除指定字符
'python'
字符串常用函數(shù)【轉(zhuǎn)換、判斷】
split() |
分割字符串 |
strip() |
去除字符串兩端空格 |
upper() |
轉(zhuǎn)大寫 |
lower() |
轉(zhuǎn)小寫 |
capitalize() |
首字母轉(zhuǎn)大寫 |
title() |
轉(zhuǎn)換為標(biāo)題模式(字符串中所有首字母大寫,其他字母小寫) |
swapcase() |
大寫轉(zhuǎn)小寫,小寫轉(zhuǎn)大寫(如:"I Like Python".swapcase() 得到i lIKE pYTHON) |
isupper() |
判斷字母是否全部為大寫 |
islower() |
判斷字母是否全部為小寫 |
istitle() |
判斷是否為標(biāo)題模式(字符串中所有單詞首字母大寫,其他小寫) |
isalpha() |
判斷字符串是否全部為字母 |
isdigit() |
判斷字符串是否全部為數(shù)字 |
isalnum() |
判斷字符串是否僅包含字母與數(shù)字 |
>>> #字符串拼接(對于+不適用的情況下可以使用)
>>> smp = "I like Python"
>>> c = smp.split(" ")
>>> c = "I like python".split()
>>> type(c) #這里檢測類型,可以看到c為列表
<class 'list'>
>>> "*".join(c)
'I*like*Python'
>>> " ".join(c)
'I like Python'
>>> " ".join(["I","Like","python"]) #這里可以直接使用列表作為join函數(shù)的參數(shù)
'I Like python'
>>> " ".join("abcd") #也可直接使用字符串作為join函數(shù)的參數(shù)
'a b c d'
>>> # count()函數(shù)統(tǒng)計(jì)指定字符串出現(xiàn)次數(shù)
>>> "like python,learn python".count("python")
2
>>> # find()函數(shù)查找指定字符串出現(xiàn)位置
>>> "python Demo".find("python")
0
>>> "python Demo".find("Demo")
7
>>> # replace(old,new)函數(shù)替換指定字符串(old)為新字符串(new)
>>> "I like php".replace("php","python")
'I like python'
簡單入門教程~
基本一看就懂~O(∩_∩)O~
未完待續(xù)~~歡迎討論??!
- Python?格式化輸出字符串的方法(輸出字符串+數(shù)字的幾種方法)
- Python格式化輸出字符串的五種方法總結(jié)
- Python如何使用print()函數(shù)輸出格式化字符串
- Python字符串三種格式化輸出
- Python字符串格式化輸出代碼實(shí)例
- Python格式化輸出字符串方法小結(jié)【%與format】
- python日期時間轉(zhuǎn)為字符串或者格式化輸出的實(shí)例
- Python實(shí)現(xiàn)字符串格式化輸出的方法詳解
- 淺談Python 字符串格式化輸出(format/printf)
- Python字符串格式化輸出方法分析
- Python中格式化字符串輸出的4種方式小結(jié)
相關(guān)文章
為2021年的第一場雪錦上添花:用matplotlib繪制雪花和雪景
這篇文章主要介紹了為2021年的第一場雪錦上添花:用matplotlib繪制雪花和雪景,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Python實(shí)現(xiàn)實(shí)時數(shù)據(jù)采集新型冠狀病毒數(shù)據(jù)實(shí)例
在本篇文章里小編給大家整理了關(guān)于Python實(shí)現(xiàn)實(shí)時數(shù)據(jù)采集新型冠狀病毒數(shù)據(jù)實(shí)例內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。2020-02-02
Python初識二叉樹續(xù)之實(shí)戰(zhàn)binarytree
binarytree庫是一個Python的第三方庫,這個庫實(shí)現(xiàn)了一些二叉樹相關(guān)的常用方法,使用二叉樹時,可以直接調(diào)用,不需要再自己實(shí)現(xiàn),下面這篇文章主要給大家介紹了關(guān)于Python初識二叉樹之實(shí)戰(zhàn)binarytree的相關(guān)資料,需要的朋友可以參考下2022-05-05
如何使用 Python和 FFmpeg 批量截圖視頻到各自文件夾中
wxPython 提供了一個簡單易用的界面,而 FFmpeg 則負(fù)責(zé)處理視頻幀的提取,這個工具不僅對視頻編輯工作有幫助,也為批量處理視頻文件提供了極大的便利,這篇文章主要介紹了使用 Python和 FFmpeg 批量截圖視頻到各自文件夾中,需要的朋友可以參考下2024-08-08
ConvNeXt實(shí)戰(zhàn)之實(shí)現(xiàn)植物幼苗分類
ConvNeXts由標(biāo)準(zhǔn)ConvNet模塊構(gòu)建,在準(zhǔn)確性和可擴(kuò)展性方面與 Transformer競爭,實(shí)現(xiàn)87.8% ImageNet top-1 準(zhǔn)確率,在 COCO 檢測和 ADE20K 分割方面優(yōu)于 Swin Transformers。本文將利用ConvNeXt實(shí)現(xiàn)植物幼苗分類,需要的可以參考一下2022-01-01
Python tkinter實(shí)現(xiàn)簡單加法計(jì)算器代碼實(shí)例
這篇文章主要介紹了Python tkinter實(shí)現(xiàn)簡單加法計(jì)算器代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
Python實(shí)現(xiàn)PS濾鏡功能之波浪特效示例
這篇文章主要介紹了Python實(shí)現(xiàn)PS濾鏡功能之波浪特效,結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)PS濾鏡波浪特效的原理與相關(guān)操作技巧,需要的朋友可以參考下2018-01-01

