python 变量转字符串_[Python Basic] 字符串处理以及类型转换 2
本節內容涉及函數稍多, 需要慢點消化, 一如既往的, 我們不僅說說 python 的最小必要知識, 也講講編程英語.
Python內置方法和函數
續接上節課,我們還可以使用Python內置的方法和函數來處理字符串。
upper()函數
upper() 函數, 將文本內的每個字符轉換并打印為大寫字符.英文:it will convert and print every character inside text in uppercase.
但是首先有個重要的提示:我們使用Python內置方法處理的字符串,不會更改字符串本身的值。But first, a very important note here. The string that we manipulate using the Python built-in >methods does NOT change the value of the string itself.
例子:
text = "Please convert me to all uppercase"
print(text.upper())
# output: PLEASE CONVERT ME TO ALL UPPERCASE
## 函數 print(text.upper()) 它將每一個字符串內的文字轉換和打印為大寫格式.
print(text)
# output: Please convert me to all uppercase
## 但是,如果我們現在打印 text 這個變量以查看其值,可以看到 text仍然具有原始的小寫字符.
由上面這個例子, 可以看出,應用于字符串的方法不會改變字符串的值。
固定變量的大寫格式
為了獲得更改后的值,需要將其分配給另一個變量,下面例子中 text_new 這個變量存儲"將文本更改為所有大寫字符"的結果,print(text_new)將打印輸出所有的大寫字符。
例子:
text = "Please convert me to all uppercase"
text_new = text.upper()
print(text_new)
# output: PLEASE CONVERT ME TO ALL UPPERCASE
lower() 函數
要將字符串轉換為小寫,可以使用 lower ()方法
例子:
text = "Please convert me to all lowercase" print(text.lower())
# output: please convert me to all lowercase
count() 函數
計算一個或多個字符出現的次數, 可以使用 count()函數的方法英文:To count the number of occurrences of a character or characters, we can use the method count().
例子:
text = "Count number of u's in me"
print(text.count("u"))
# output : 3
輸出文本中u這個字符的數量是 3 個.
replace() 函數
要將一個或多個字符替換為其他字符,可以使用replace()的函數方法英文:To replace a character or characters with something else, we can use the method replace()
例子:
text = "Replace this with that"
print(text.replace("this", "that"))
# output : Replace that with that
例子中將原來的 this 替換為 that.
len() 函數
要查找文本的長度,我們可以使用 Python 內置函數 len().英文:To find the length of a text, we can use the Python >built-in function len()
例子:
text = "What is the length of this text"
print(len(text))
# output : 31
這是字符串的長度, 31 ,包含空格
strip() 函數
要刪除文本兩端的空白,可以使用方法strip().英文:To remove both ends of a text of its whitespaces, we can use the method strip()
例子: text 左右各三個空格,共 21 字符的字符串
text = " Strip both ends "
print(text.strip())
# output: Strip both ends
print(len(text.strip()))
# output: 15
實際輸出句子兩側無空格
lstrip() 函數
要刪除"前置空格",我們可以使用lstrip()方法.英文:To remove leading whitespaces, we can use the method lstrip().
例子:3個前置空格和3個尾隨空格
text = " Strip left end "
print(text.lstrip())
# output : Strip left end
## 將打印“Strip left end空格空格空格”,去掉前置空格,保留尾隨空格.
rstrip() 函數
要刪除尾隨空格,我們可以使用rstrip()方法英文:To remove trailing whitespaces, we can use the method rstrip()
例子:text 字符串包含3個前置空格和3個尾隨空格
text = " Strip left end "
print(text.rstrip())
# output : Strip left end
## 將打印“空格空格空格Strip left end”,保留前置空格,去掉尾隨空格
is**()函數
以字符 i s 開頭的方法, 將返回布爾值 True 或 False。
英文:Methods starting with the characters i s, will return the Boolean value of either True or False. isalnum() checks that EVERY character in the text is an alphanumeric character isalpha() checks that EVERY character in the text is an alphabetic character isdigit() checks that EVERY character in the text is a numeric character isupper() checks that EVERY character in the text is an uppercase character islower() checks that EVERY character in the text is a lowercase character
例子1:
text = "abcdEf"
print(text.isalnum())
# output: True
## 因為每個字符是一個字母數字
print(text.isalpha())
# output: True
## 因為每個字符是一個字母字符
print(text.isdigit())
# output: False
## 因為所有字符都不是數字字符
print(text.isupper())
# output: False
## 因為只有E字符為大寫
print(text.islower())
# output: False
## 因為即使所有字符都為小寫字母,但E字符仍為大寫字母
例子 2:
text = "12345"
print(text.isalnum())
# output: True
## 因為每個字符都是字母數字字符
print(text.isalpha())
# output: False
## 因為所有字符都是數字
print(text?isdigit())
# output: True
## 因為每個字符都是數字字符
print(text.isupper())
# output: False
## 因為所有字符都是數字,所以所有字符都不是大寫
print(text.islower())
# output: False
## 因為所有字符都是數字,所以所有字符都不是小寫
例子 3:
text = "abc def"
print(text.isalnum())
# output: False
## 因為它包含空格字符,而不是字母數字字符
print(text.isalpha())
# output: False
## 因為它包含空格字符,而不是字母字符
完畢, 待總結。
發布時間: 2020 年 2 月 17 日
當前可任意轉載,轉載請保存以上信息即可。無需獲得授權.
總結
以上是生活随笔為你收集整理的python 变量转字符串_[Python Basic] 字符串处理以及类型转换 2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c# socket接收字符串_socke
- 下一篇: 于python保留字的是_《于》字意思读