python基础之字符串内置方法练习

1.将字符串 “abcd” 转成大写
2.计算字符串 “cd” 在 字符串 "abcd"中出现的位置
3.字符串 “a,b,c,d” ,请用逗号分割字符串,分割后的结果是什么类型的?
4."{name}喜欢{fruit}".format(name=“李雷”) 执行会出错,请修改代码让其正确执行
5.string = “Python is good”, 请将字符串里的Python替换成 python,并输出替换后的结果
6.有一个字符串 string = “python修炼第一期.html”,请写程序从这个字符串里获得.html前面的部分,要用尽可能多的方式来做这个事情
7.如何获取字符串的长度?
8.“this is a book”,请将字符串里的book替换成apple
9.“this is a book”, 请用程序判断该字符串是否以this开头
10.“this is a book”, 请用程序判断该字符串是否以apple结尾
11.“This IS a book”, 请将字符串里的大写字符转成小写字符
12.“This IS a book”, 请将字符串里的小写字符,转成大写字符
13.“this is a book\n”, 字符串的末尾有一个回车符,请将其删除
#1
a = "abcd"
print(a.upper())
ABCD
#2
"abcd".find("cd")
2
#3
"a,b,c,d".split(",")
['a', 'b', 'c', 'd']
"a.b.c.d".split(".")
['a', 'b', 'c', 'd']
#4
"{name}喜欢{fruit}".format(name="李雷",fruit="芒果")
'李雷喜欢芒果'
#5
string = "Python is good"
string.replace("Python","python")
'python is good'
#6
string = "python修炼第一期.html"
print(string[:11])
print(string[0:11])
print(string[-16:-5])
print(string.split(".")[0])
python修炼第一期
python修炼第一期
python修炼第一期
python修炼第一期
#7
string = "bianxia"
print(len(string))
7
#8
string = 'this is a book'
string.replace("book","apple")

'this is a apple'
#9
string = "this is a book"
string.startswith("this")
True
#10
string = "this is a book"
string.endswith("apple")
False
#11
string = "This IS a book"
string.lower()
'this is a book'
#12
string = "This IS a book"
string.upper()
'THIS IS A BOOK'
#13
string = "this is a book\n"
string.strip()
'this is a book'

第二题可以直接使用find

第13小题,strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列, \n 就是换行符

参考了python基础130道练习题
  • 5
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值