String
- str
- Text information
- Single double quotation marks, three quotes
three characteristics quoted that can store more than one line, and keep the format
a= '''
i
asd
asd
'''
print(a)
i
asd
asd
Escape character
- Escaped with a backslash
- the windows \ n newline; Linux in \ r \ n: line feed
a='wo'
b='ai'
print(a+b)
woai
a= "let's go"
b= 'let\' go'
print(a,b)
# print(a,\nb) 错误
File "<ipython-input-12-ec162cdbe208>", line 4
print(a\n,b)
^
SyntaxError: unexpected character after line continuation character
- Want to show \, you need to use \ \ backslash backslash transfer
a = 'abc\ndef\nghijk'
print(a)
abc
def
ghijk
Common escape character
\ line continuation, representation format is too long, can be used to write the next line,
# 续行
for b in\
range(1,10):
print(b,end=' ')
1 2 3 4 5 6 7 8 9
format
- String printed or filled in a format
fixed, the place is filled with Variational - Fill mode
- Traditional format, occupy a position with a placeholder%
but predetermined format
% s: string representing the placeholder
% f: indicates floating placeholder
% .2f represents two decimal places
- Traditional format, occupy a position with a placeholder%
Similarly decimal places in python
比如format中,“aaa {:.2f} asd”
在里面加上冒号,表示对里面的要求,然后加上 数字+f
- format 没有规定必须是什么格式
但是:字符串需要加上引号
- format时使用{}来占位的,那如果想表示{}
s = "format是使用{}来占位的"
print(s)
format是使用{}来占位的
a = “I love %s”
print(a)
print(a%“XXX”)
I love %s
I love XXX
b = 'XXX'
a = "I love %s" #单引号和双引号的字符串可以公用
print(a%b)#把这个式子展开就变为↓
print("I love %s"%'XXX')
# 和format有点像
print("I love {}".format(100))
# 将上面的写为多行
a = "I love {}"
b = input()
print(a.format(b))
I love XXX
I love XXX
I love 100
XSA
I love XSA
a = "{} {} !"
b = input()
c = input()
a = a.format(b,c)
print(a)
# print("{} {} !".format(b,c))
a
s
a s !
s = "{2}{1}{0}"
print(s.format("a","b","c"))#format里面的需要时字符串,字符串需要加上引号
cba
Named parameters
s = "a is {aa} , b is {bb} , c is {cc}"
print(s.format(aa="a",bb="b",cc="c"))
a is a , b is b , c is c
dictionary
- Need prefixed with **, unpack the dictionary
- Dictionary format { "": "", "": ""}
a_dict= {"a":"aa","b":"bb","c":"cc"}
s = "a is {a},b is {b},c is {c}"
print(a_dict)
print(s.format(**a_dict))
{'a': 'aa', 'b': 'bb', 'c': 'cc'}
a is aa,b is bb,c is cc
built-in function str
- Python represented by the string str
- There are many built-in functions
Find class find, index
- find Usage: is the result returned is the position of characters to find the first occurrence, regardless of the latter, -1 is returned if no
- Also start from the desired range to find
- rfind and lfind, start looking for the right, from the left to find
- index and find similar, except that the index can not find out the character will complain, throw an exception
Analyzing class islower, isalpha
- isalpha,判断字符串是否是字母,不能区分中英文
- 判断是否全部是字母
- 空格和逗号都不是
- S.isalpha() ,并返回结果(bool)
- islower 判断是否全是小写的
- 可以help(str。islower)
- 空格不影响
- 汉字不是小写
- 汉字也不是大写
- “阿瑟东”。isuper()
Content judge
- startswith/endswith
- Check whether the beginning or end of the string to XXX
- ”asdasdzxcqwe“。startswith(”asd”)
- Return result (bool)
- Similarly you can know, start, end
- “asdqweasd”。start(“qwe”)
Operating class
- format
- strip:删除字符串两边的空格或者字符,如果在后面逗号加上字符串,那么就会删掉想要的字符串
- 不返回值
- 只会从两边开始删除,并且山字符串空格不会删掉
- rstrip,lstrip,删掉右边的,删掉左边的
- join:将两个字符串进行拼接,主要是以逗号为唤起点
- print(“a”。join(“as,阿瑟东,请问,q”))
- 结果是“a”将后面的字符串中,逗号左右都加上一个“a”
- 如果是对列表进行,则是将列表中的逗号进行替换
s = "asdasdasdqasdasdqwe"
s1 = "q"
# s.find(s1)
# s.find(s1,10) #表示从10的位置开始查找
s.rfind(s1) # 从右边开始查找,查处的结果是正序的位置
16
# strip
a = "asdhho qweoi kjh "
print(a)#看不出来
print(a.strip())
print(a,end='---')
print()
print(a.strip(),end="---")
print()
print(a.strip("asd"),end="---")
asdhho qweoi kjh
asdhho qweoi kjh
asdhho qweoi kjh ---
asdhho qweoi kjh---
hho qweoi kjh ---
print("a".join("a,d,f,q,q"))
print("a".join(["a","d","f","q","q"]))
aa,ada,afa,aqa,aq
aadafaqaq