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())

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值