1. 程式人生 > >python字串string的內建方法

python字串string的內建方法

#__author: "Pizer Wang"
#__date: 2018/1/28

a = "Let's go"
print(a)
print("-------------------")

a = 'Let\'s go'
print(a)
print("-------------------")

print("hello" * 3)
print("helloworld"[2:])
print("-------------------")

print("ell" in "helloworld")
print("-------------------")

print("Pizer is a good student"
) print("%s is a goog student" % "Pizer") print("-------------------") a = "1234" b = "abcd" c = "[email protected]#$" d = a + b + c print(d) d = "".join([a, b, c]) print(d) d = ", ".join([a, b, c]) print(d) d = "++".join([a, b, c]) print(d) print("-------------------") print("string的內建方法") str = "helloworld"
print(str.count("l")) #統計元個數 print(str.capitalize()) #首字母大寫 print(str.center(25, "-")) #居中 print(str.endswith("d")) print(str.endswith("world")) print(str.endswith("word")) #是否以某個內容結尾 print(str.startswith("hello")) #是否以某個內容開始 str = "hello\tworld" print(str.expandtabs(tabsize=10
)) print("-------------------") str = "helloworld {name} is {age}" print(str.find("w")) #查詢到第一個元素並將索引值返回 print(str.format(name = "Pizer", age = 18)) print(str.format_map({"name":"Jone", "age":25})) print("-------------------") print(str.index("w")) #print(str.index("www")) #報錯 print(str.find("wwww")) print("-------------------") str = "123abc" print(str.isalnum()) str = "123" print(str.isalnum()) str = "abc" print(str.isalnum()) str = "[email protected]$" print(str.isalnum()) str = "中國萬歲" print(str.isalnum()) print("-------------------") print("123456".isdecimal()) print("123456ff".isdecimal()) print("123456789".isdigit()) print("12345.6789".isdigit()) print("12345.6789".isnumeric()) print("-------------------") print("34abc".isidentifier()) print("_34abc".isidentifier()) print("abc".islower()) print("abC".islower()) print("ABC".isupper()) print(" ".isspace()) print("-------------------") print("Hello Jone".istitle()) print("Good morning".istitle()) print("-------------------") print("Hello Jone".lower()) print("Good morning".upper()) print("Hello Jone".swapcase()) print("-------------------") print("Hello world".ljust(20, "-")) print("Hello world".rjust(20, "-")) print(" Hello world \t \n") print(" Hello world ".strip()) print(" Hello world ".lstrip()) print(" Hello world ".rstrip()) print("-------------------") print("Hello Jone Jone".replace("Jone", "Pizer")) print("Hello Jone Jone".replace("Jone", "Pizer", 1)) print("My title".find("t")) print("My title".rfind("t")) print("-------------------") print("Hello world".split(" ")) print("Hello world".split("l", 1)) print("Hello world".rsplit("l", 1)) print("hello jone".title()) print("-------------------") #重要的字串方法 # print(st.count('l')) # print(st.center(50,'#')) # 居中 # print(st.startswith('he')) # 判斷是否以某個內容開頭 # print(st.find('t')) # print(st.format(name='alex',age=37)) # 格式化輸出的另一種方式 待定:?:{} # print('My tLtle'.lower()) # print('My tLtle'.upper()) # print('\tMy tLtle\n'.strip()) # print('My title title'.replace('itle','lesson',1)) # print('My title title'.split('i',1))

執行結果:

Let’s go

Let’s go

hellohellohello

lloworld

True

Pizer is a good student

Pizer is a goog student

[email protected]#[email protected]#
1234, abcd, [email protected]#$

[email protected]#$

string的內建方法
3
Helloworld
——–helloworld——-
True
True
False
True

hello world

5
helloworld Pizer is 18

helloworld Jone is 25

5

-1

True
True
True
False

True

True
False
True
False

False

False
True
True
False
True

True

True

False

hello jone
GOOD MORNING

hELLO jONE

Hello world———
———Hello world
Hello world

Hello world
Hello world

Hello world

Hello Pizer Pizer
Hello Pizer Jone
3

5

[‘Hello’, ‘world’]
[‘He’, ‘lo world’]
[‘Hello wor’, ‘d’]

Hello Jone

相關推薦

Python ——字串方法

字串的內建方法 上一篇文章只是介紹了字串的轉義和格式化,本篇文章著重介紹一下字串的內建方法和基本操作。字串的內建方法有30種左右,本篇簡單介紹一下常用的幾種。想了解更多,請自行移步求助偉大的 help()。 基本操作 1:字串的拼接 字串加字串的語法,在Python中成為

Python 字串 String 函式大全(1)

關於 Python 的字串處理相關的方法還是非常多的,由於我正在學習 Python,於是就把 Python 中這些混雜的用於 string 的函式總結出來,在自己忘記的時候便於查詢,希望對於有類似需求的人有所幫助。 captalize() 函式

python中的字串以及方法

字串的操作方法 +  字串連線操作 str1 = "大金鍊子" str2 = "小手錶" str3 = str1 + str2 print(str3) *  字串複製操作 str1 = "大金鍊子" str2 = "小手錶" str3 = str1*3

python學習day6 for迴圈 字串方法

1.for迴圈 和while相比 l=[1,2,3] i=0 while i <len(l)   print(l[i])   i+=1   l=['a','b','c'] for item in l:   print(item) 字典中的應用:   dic={'x':1

字串的大小,python的常用方法

判斷字串 變成‘標題’ In [1]: ‘Hello’.istitle() 當首字母為大寫時,計算機則認為其為標題 Out[1]: True In [2]: ‘hello’.istitle() Out[2]: False In [7]: ‘heLLo’.islower() 當全部為小

python裡的字串常用方法和格式化操作

字串(str)的一些內建操作方法:capitalize() 將字串的首個字母變為大寫casefold() 把全部的大寫全部變為小寫center(width) 將字串居中, 並用空格填充長度至width的新字串的長度count(sub[, start[, end]]) 返回s

Python開發 標準方法 (未完代補)

back iter sci side pty 之前 follow cal 開發 abs(number) 絕對值 The abs() method takes a single argument:    num - number whose absolute valu

Day06for迴圈和字串方法

Day06 1.for迴圈(迭代器迴圈) while迴圈 條件迴圈,迴圈是否結束取決於條件的真假 for迴圈,迭代器迴圈,多用於迴圈取值,迴圈是否結束取決於被迴圈資料的元素個數 2.range(1,5) 取值顧頭不顧尾,python2中為列表[1,2,3,4] python3 中為rang

python的常用方法

求最小值、最大值和求和 In [1]: min(2,4) Out[1]: 2 In [2]: max(2,4) Out[2]: 4 In [3]: sum(range(1,101)) Out[3]: 5050 In [4]: sum(range(2,101,2)) Out[4]:

列表和字串方法

列表中轉悠的內建方法: 1、append()  #在列表末尾新增新的物件 2、count()   #統計某個元素在列表中出現的次數 3、extend()    #在列表末尾一次性追加另一個序列中的多個值(用新列表擴充套件原來的列表) 4、index()   #從列表中找出某個值第一個匹配項的

字串常用方法-python3

我們常常要對字串進行處理,那麼看看python3中都有哪些內建方法呢? String.capitalize():將字串的第一個字母大寫 String.count():獲得字串中某一子字串的數目 String.find():獲得字串中某一字串的起始位置 String.isa

Python中常用方法:__str__,__repr__使用詳解

     因為python中所有類預設繼承object類。而object類提供了了很多原始的內建屬性和方法,所以使用者自定義的類在Python中也會繼承這些內建屬性。可以使用dir()函式可以檢視,雖然

python字串string方法

#__author: "Pizer Wang" #__date: 2018/1/28 a = "Let's go" print(a) print("-------------------") a = 'Let\'s go' print(a) print("-

python關於字串方法

1. str.split() Return a list of the words in the string, using sep as the delimiter string. sep The delimiter according which to split the s

python資料型別(string/list/tuple/dict)方法

Python 字串常用方法總結 明確:對字串的操作方法都不會改變原來字串的值 1,去掉空格和特殊符號 name.strip()  去掉空格和換行符 name.strip('xx')  去掉某個字串 name.lstrip()  去掉左邊的空格和換行符

Python 字串方法(一)

以下方法只需要知道用法就行了,權當了解,不用硬背,以後需要用到的時候再回來看 說明: 一般方法中前面是is開頭的就是判斷來的,輸出不是True就是False,例如isalpha()方法 capitalize()方法:首字母大寫 In [1]: a='abc' In [2]: a.capit

第014講:字串:各種奇葩的方法 | 學習記錄(小甲魚零基礎入門學習Python

(標答出處: 魚C論壇) 《零基礎入門學習Python》 測試題: 還記得如何定義一個跨越多行的字串嗎(請至少寫出兩種實現的方法)? 例一:str = (‘春眠不覺曉, 處處聞啼鳥。 也來風雨聲, 花落知多少。’) 例二:str = ‘春眠不覺曉, 處處聞啼鳥。 也來風雨聲

入門學習-Python-小甲魚學習資料-Day014-字串:各種奇葩的方法

根據視訊自己練習 : capitalize() 把字串的第一個字元改為大寫 casefold() 把整個字串的所有字元改為小寫 center(width) 將字串居中,並使用空格填充至長度 width 的新字串 count(sub[, start[, end]]) 返回 sub 在字串裡

Python字串方法

capitalize()方法返回字串的一個副本,只有它的第一個字母大寫。對於8位的字串,這個方法與語言環境相關。 capitalize語法 以下是capitalize()方法的語法: str.capitalize() 引數 NA 返回值 此方法返回的字串只有它的第一個字元

零基礎入門學習Python(14)--字串:各種奇葩的方法

前言 這節課我們回過頭來,再談一下字串,或許我們現在再來談字串,有些朋友可能覺得沒必要了,甚至有些朋友就會覺得,不就是字串嗎,哥閉著眼也能寫出來,那其實關於字串還有很多你不知道的祕密哦。由於字串在日常生活中是如此的常見,因此小甲魚抱著負責任的態度,在這節課上,