python学习笔记10(strings part2)

知识点1: 当+号应用于字符串当中是,它表达的意思就是"级联"

a = 'Gary'
b = 'Hello '+ a
print(b)

知识点2: 将in作为一个逻辑运算符

c = 'Good man!'
'c' in c
'oo' in c
if 'oo' in c:
    print('nice!')

in的作用是判断内容是否在字符串当中,并返回True和False,同样适用在if当中,可以用来判断。

知识点3: 字符串的比较功能

word = input('Enter a word')

if word == 'banana':
    print('Oh,that is banana')
elif word < 'banana':
    print(word + ' banana')
elif word > 'banana':
    print('banana ' + word)

大写字母是比小写字母要小的。

知识点4: String Library

  • 字符串库可以理解为是一种内嵌的函数功能
  • Python在字符串库中有一系列的字符串函数
  • 这些功能事先已经被内嵌在字符串当中了,我们通过对字符串变量的追加来进行调用
  • 最重要的是字符串库的功能是不改变字符串本身的内容的,它返回的知识一个修改过后的副本
greet = 'Hello Gary!'

print(greet.lower())  #变成小写的功能
print(greet)
print('HI,THERE!'.lower())

输出结果为:
hello gary!
Hello Gary!
hi,there!

.lower()就是一个Object method对象方法

知识点5: 初识类与对象
首先看一段代码:

stuff = 'Hello World'
print(type(stuff))
print(dir(stuff))

输出结果为:
<class ‘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’]

对象就是字符串stuff,类就是指str,而dir所显示的就是对象方法object methods
常见的 object method:
在这里插入图片描述

扫描二维码关注公众号,回复: 5956872 查看本文章
  • str.capitalize():首字母大写
  • str.find():用于寻找字符串中的相关内容,有一点类似于in,但是in是返回True和False,.find返回的是内容在字符串当中的位置,如果没有找到,就返回-1.例如:
str1 = 'Gary is a good man'
print(str1.find('is'))
print(str1.find('yes'))

输出结果为:
5
-1

  • str.replace()用于找到字符串中的相关内容并用给定的内容代替它,例如:
str2 = 'Gary is a good man'
print(str2.replace('good' , 'handsome'))

输出结果为:
Gary is a handsome man

  • str.lstrip()

  • str.rstrip()

  • str.strip()
    分别用于消除字符串左边的所有空格,字符串右边的所有空格,以及字符串左右的所有空格,例如:

  • str.startswith()用于判断是否是某个前缀开头。

str4 = 'Gary is a good man'
print(str4.startswith('Gary'))
print(str4.startswith('gary'))

输出结果为:
True
False

知识点6: 解析与提取(parse and extract)
例:提取一段字符串中的某一段内容:

info = 'Gary email [email protected]'  #准备提取[email protected]
num1 = info.find(' ')
print(num1)
num2 = info.find(' ' , num1+1)  #第二个参数很重要,意思是从这个位置之后再开始找
print(num2)
num3 = info.find('m' , num2)
print(num3)
print(info[num2:num3+1])

输出结果为:
4
10
26
[email protected]
Tip:一开始调试的时候,写了num1,而不是num1+1,这样显示的还是4,而不是10,原来是这个参数的意思是从这个位置开始查找, 是包括这个位置的,恰巧这个位置还是空格,所以还是显示4。

这里又牵涉到一个py2和py3的区别,py3字符串使用的是统一码,都是str类型,而py2可能会出现str与unicode相互转换的情况发生。

在这里插入图片描述
The documentation uses a syntax that might be confusing.
For example, in find(sub[, start[, end]]), the brackets indicate optional arguments. So sub is required, but start is optional, and if you include start, then end is optional.

猜你喜欢

转载自blog.csdn.net/weixin_43593303/article/details/89260144
今日推荐