Python进阶16_字符串基础操作

前面介绍了一下比较晦涩而且深入的字符文本的处理方法,接下来几节将带来一些常规的操作。比较容易入手。

字符串处理

字符串字面量

字符字面量的通过使用:以单引号开始和结束,但是如何处理的是字符串内使用单引号。
例如:'That is Wali's cat

双引号

字符字面量的还可以通过使用:双引号开始和结束,这样就可以处理字符串内使用单引号的问题.
如下:"That is Wali's cat"

转义字符

“转义字符”让你输入一些字符,它们用其他方式是不可能放在字符串里的.
转义字符包含一个倒斜杠(\),紧跟着是想要添加到字符串中的字符.转义字符’和"让你能在字符串中加入单引号和双引号
常见的转义字符如下表:

转义字符

原始字符串

以在字符串开始的引号之前加上 r,使它成为原始字符串。“原始字符串”完全忽略所有的转义字符,打印出字符串中所有的倒斜杠。Python 认为倒斜杠是字符串的一部分,而不是转义字符的开始

用三重引号的多行字符串

多行字符串的起止是 3 个单引号或 3 个双引号。“三重引号”之间的所有引号、制表符或换行,都被认为是字符串的一部分。

多行注释

虽然井号字符(#)表示这一行是注释,但多行字符串常常用作多行注释,

字符串下标和切片

字符串像列表一样,使用下标和切片.通过切片并将结果子字符串保存在另一个变量中,就可以同时拥有完整的字符串和子字符串

字符串的 in 和 not in 操作符

像列表一样,in 和 not in 操作符也可以用于字符串.

spam = "That is Wali's cat"
print(spam)

# 转义字符 
spam2 = 'That is Wali\'s cat'
spam3 = 'That is Wali\'s cat \nHow are you? \nI\'m fine.'
print(spam2)
print(spam3)


# 原始字符串 
print(r'That is Wali\'s cat')

# 三重引号的多行字符串 

print('''Dear Alice, 


Eve's cat has been arrested for catnapping, cat burglary, and extortion. 


Sincerely, 
Bob''') 

# 字符串下标和切片

print(spam[0])
print(spam[1:3])
print(spam[:5])
print(spam[6:])

# 字符串的 in 和 not in 操作符 
print("wali" in spam)
print("wali" not in spam)
print("Wali" in spam)
That is Wali's cat
That is Wali's cat
That is Wali's cat 
How are you? 
I'm fine.
That is Wali\'s cat
Dear Alice, 


Eve's cat has been arrested for catnapping, cat burglary, and extortion. 


Sincerely, 
Bob
T
ha
That 
s Wali's cat
False
True
True

常用的字符串方法

upper()、lower()、isupper()和 islower()
  • upper(): 将字符串中的字母转换为大写字母
  • lower():将字符串中的字母转换为小写字母
  • isupper(): 判断字符串中的字母是否都是大写字母
  • islower() :判断字符串中的字母是否都是小写字母

注意点: 比如spam 是一个一个字符串,当使用spam.upper()的时候,会生成一个新的字符串,而不会去改变spam字符串本身。

upper()与lower()的使用场景:
在字符串进行无关大小写比较的时候,使用该方法很有效。

spam = 'Wali AI'
spamUp = spam.upper()
print('spam:',spam)
print('spamUp:',spamUp)
spam = spam.lower()
print('spam:',spam)


spam: Wali AI
spamUp: WALI AI
spam: wali ai
#大小写无关的比较
print('How are you?')
feeling = input()
if feeling.lower() == 'great':
    print('I feel great too.')
else:
    print('I feel liitle bad.')
How are you?
great
I feel great too.
#如果字符串至少有一个字母,并且所有字母都是大写或小写
spam = '12345'
print(spam.isupper())
print(spam.islower())

spam = 'a12345B'
print(spam.isupper())
print(spam.islower())
spam = spam.upper()
print(spam.isupper())
False
False
False
False
True
isX 字符串方法

isX 该类型的方法用来判断字符的类型和属性,返回一个布尔值。

  • isupper(): 判断字符串中的字母是否都是大写字母
  • islower() :判断字符串中的字母是否都是小写字母
  • isalpha(): 判断字符串是否只包含字母,并且非空
  • isalnum(): 判断字符串是否只包含字母,数字,并且非空
  • isdecimal(): 判断字符串是否只包含数字,并且非空
  • isspace(): 判断字符串是否只包含空格,制表符和换行,并且非空
  • istitle(): 判断字符串是否只包含以大写字母开头,后面都是小写字母。

使用的场景:
对于一些需要验证字符信息是否符合规范的方式,比如验证用户输入的信息

print('wali'.isalpha())
print('wali12'.isalpha())
print('wali12'.isalnum())
print('wali'.isnumeric())
print('wali'.isdecimal())
print('\t'.isspace())
True
False
True
False
False
True
# 使用的场景

while True:
    print('Enter your age:')
    age = input()
    
    if age.isdecimal() and int(age) > 0:
        break
    else:
        print("please enter right number")
 
Enter your age:
-1
please enter right number
Enter your age:
32
startswith()和 endswith()

用来判断字符串的开头和结尾的字符串是否与传入的字符相同。这样就可以替代==操作符。

#startswith()和 endswith() 

spam = "I watch wali ai."

print(spam.startswith('I'))
print(spam.startswith('I w'))
print(spam.startswith('I watch'))
print(spam.startswith('I watch wa'))
print(spam.endswith('ai.'))
True
True
True
True
True
join()和 split()

join():将一个字符串列表拼接成一个字符串,但是调用该方法的字符串将会插入到字符串之间.

split(): 将一个字符串分割成一个字符串列表,分割的方式按照传入的标志开始分割.常见的用法是:分割多行字符串. spam.split('\n')

#  join()和 split() 

lsStr = ['I','follow','Wali','AI']

print(' '.join(lsStr))
print(','.join(lsStr))
print('ABC'.join(lsStr))

print('IABCfollowABCWaliABCAI'.split('ABC'))

spam = '''Dear Alice, 
How have you been? I am fine. 
There is a container in the fridge 
that is labeled "Milk Experiment". 
 
Please do not drink it. 
Sincerely, 
Bob''' 

print(spam.split('\n'))

I follow Wali AI
I,follow,Wali,AI
IABCfollowABCWaliABCAI
['I', 'follow', 'Wali', 'AI']
['Dear Alice, ', 'How have you been? I am fine. ', 'There is a container in the fridge ', 'that is labeled "Milk Experiment". ', ' ', 'Please do not drink it. ', 'Sincerely, ', 'Bob']
用 rjust()、ljust()和 center()方法对齐文本

rjust()ljust() 用来对齐,左对齐与右对齐,传入的参数是用来表示需要填充的字符个数和以什么方式填充.

center() 居中对齐.

如果需要打印表格式数据,留出正确的空格,这些方法就特别有用.

spam = "Wali"
print(spam.rjust(10,'*'))
print(spam.ljust(10,'*'))
print(spam.center(10,'*'))

# 多余填充的长度就不响应该方法
spam = 'I follow wali AI.'
print(spam.rjust(10,'*'))
print(spam.ljust(10,'*'))


******Wali
Wali******
***Wali***
I follow wali AI.
I follow wali AI.
用 strip()、rstrip()和 lstrip()删除空白字符

strip() 用来删除字符串左右两边的空白字符

  • 空格
  • 制表符
  • 换行符

还可以通过传入字符串参数,来指定需要删除指定的字符串参数.

spam = '''  Wali 
    AI
   '''  
print(spam.strip())
print(spam.lstrip())
print(spam.rstrip())

spam = 'SpamSpamBaconSpamEggsSpamSpam' 
spam.strip('ampS')
Wali 
    AI
Wali 
    AI
   
  Wali 
    AI





'BaconSpamEggs'

分享关于人工智能,机器学习,深度学习以及计算机视觉的好文章,同时自己对于这个领域学习心得笔记。想要一起深入学习人工智能的小伙伴一起结伴学习吧!扫码上车!

瓦力人工智能 - 扫码上车

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值