sql.append 使用有空格的字符串_[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。

945bad7b951fe9c8600cd1ac0dc96e2b.png
英文: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 日

知乎链接: 字符串处理以及类型转换 2

当前可任意转载,转载请保存以上信息即可。无需获得授权.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值