Python基础-1.字符串处理
1. 用引号表示的都是字符串,引号可以是单引号',也可以是双引号".
>>test = 'hello,world!'
>>print(test)
hello,world!
>>test = "Hello,world!"
>>print(test)
hello,world!
>>test = '"hello",world!'
>>print(test)
"hello",world!
>>test = "'hello',world!"
>>print(test)
'hello',world!
注:连续的三个单引号或者双引号用来写注释
```
*****
```
"""
****
````
2.修改字符串大小写
2.1 string.title()将每个单词的首字母大写
>>test = 'hello,world!'
>>print(test.title())
Hello,World!
2.2 string.upper()将每个字符转化为大写
>>test = 'hello,world!'
>>print(test.upper())
HELLO,WORLD!
2.3 string.lower()将每个字符转化为小写
>>test = 'HELLO,World!'
>>print(test.lower())
hello,world!
3. 拼接字符串string1+string2
>>test = 'hello' + 'world!'
>>print(test)
helloworld!
4.添加空白
制表符'\t' 空格' ' 换行符'\n'
>>test = 'hello\tworld\n!'
>>print(test)
hello world
!
>>test = 'hello world\n!'
>>print(test)
hello world
!
5.删除空白
string.lstrip()删除字符串左边的空白字符
string.rstrip()删除字符串右边的空白字符
sstring.strip()删除字符串左右两边的所有空白字符
>>test = ' hello world! '
>>print(test.lstrip())
hello world!
>>print(test.rstrip())
hello world!
>>print(test.strip())
hello world!
6.切片处理
string.split()
test = ' hello world! '
print(test.split())