本文实例讲述了Python学习笔记之字符串和字符串方法。分享给大家供大家参考,具体如下:
字符串
在 python 中,字符串的变量类型显示为 str
。你可以使用双引号 "
或单引号 '
定义字符串
定义字符串
my_string = 'this is a string!' my_string2 = "this is also a string!!!" # Also , we can use backslash '/' to escape quotes. this_string = 'Simon\'s skateboard is in the garage.' print(this_string)
字符串的常用操作
first_word = 'Hello' second_word = 'There' print(first_word + second_word) # HelloThere print(first_word + ' ' + second_word) # Hello There print(first_word * 5) # HelloHelloHelloHelloHello print(len(first_word)) # 5 print(first_word[0]) # H print(first_word[1]) # e
字符串[相关练习]
在字符串中正确的使用引号
ford_quote = 'Whether you think you can, or you think you can\'t--you\'re right.' print(ford_quote) # Whether you think you can, or you think you can't--you're right.
下面这段代码的输出是什么?
coconut_count = "34" mango_count = "15" tropical_fruit_count = coconut_count + mango_count print(tropical_fruit_count) # 3415 (并且 tropical_fruit_count 是字符串)
编写服务器日志消息
username = "Kinari" timestamp = "04:50" url = "http://petshop.com/pets/mammals/cats" # TODO: print a log message using the variables above. The message should have the same format as this one: "Yogesh accessed the site http://petshop.com/pets/reptiles/pythons at 16:20." print(username + ' accessed the site ' + url + ' at ' + timestamp + '.')
使用字符串连接和 len
函数计算某些电影明星的实际完整姓名的长度
given_name = "William" middle_names = "Bradley" family_name = "Pitt" name_length = len(given_name + ' ' + middle_names + ' ' + family_name) # Now we check to make sure that the name fits within the driving license character limit driving_license_character_limit = 28 print(name_length <= driving_license_character_limit) # True
我们刚刚使用函数 len
计算出字符串的长度。当我们向其提供整数 835 而不是字符串时,函数 len
会返回什么?
Error
字符串方法
python 中的方法和函数相似,但是它针对的是你已经创建的变量。方法特定于存储在特定变量中的数据类型。
每个方法都接受字符串本身作为该方法的第一个参数。但是,它们还可以接收其他参数。我们来看看几个示例的输出。
my_string = "sebastian thrun" my_string.islower() # True my_string.count('a') # 2 my_string.find('a') # 3
可以看出,count
和 find
方法都接受另一个参数。但是,islower
方法不接受参数。如果我们要在变量中存储浮点数、整数或其他类型的数据,可用的方法可能完全不同!
字符串方法[相关练习]
- 对浮点型对象调用
islower
等方法会发生什么?例如13.37.islower()
- 会出现错误, 方法
islower
属于字符串方法,而不是浮点数方法。不同类型的对象具有特定于该类型的方法。例如,浮点数具有is_integer
方法,而字符串没有。 - 练习字符串方法
my_name = "my name is Joh." cap = my_name.capitalize() print(cap) # My name is joh. ew = my_name.endswith('li') print(ew) # False ind = my_name.index('is') print(ind) # 8
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python字符串操作技巧汇总》、《Python数据结构与算法教程》、《Python列表(list)操作技巧总结》、《Python编码操作技巧总结》、《Python函数使用技巧总结》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。
Python,字符串,字符串方法
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】