python练习题-数据类型-字符串方法-字符串习题练习(七)
#coding=utf-8 #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", 字符串的末尾有一个回车符,请将其删除 print "abcd".upper(); print "abcd".count("cd"); print "a,b,c,d".split(",") #得到一个元素是字符的列表 print "{name}喜欢{fruit}".format(name="李雷", fruit='苹果') print "Python is good".replace("Python","python"); print "python修炼第一期.html"[0:-5]; print "7题:", len("abc"); print "8题:","this is a book".replace("book","apple"); print "9题:","this is a book".startswith("this"); print "10题:","this is a book".endswith("apple"); print "11题:","This IS a book".lower(); print "12题:","This IS a book".upper(); print "13题:","this is a book\n".rstrip("\n"); print "13题:","this is a book\n".strip("\n");
运行结果如下:
ABCD 1 ['a', 'b', 'c', 'd'] 李雷喜欢苹果 python is good python修炼第一期 7题: 3 8题: this is a apple 9题: True 10题: False 11题: this is a book 12题: THIS IS A BOOK 13题: this is a book 13题: this is a book