Python学习笔记之字符串和字符串方法实例详解


Posted in Python onAugust 22, 2019

本文实例讲述了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 中的方法和函数相似,但是它针对的是你已经创建的变量。方法特定于存储在特定变量中的数据类型。

Python学习笔记之字符串和字符串方法实例详解
注:图片来源网络

每个方法都接受字符串本身作为该方法的第一个参数。但是,它们还可以接收其他参数。我们来看看几个示例的输出。

my_string = "sebastian thrun"
my_string.islower() # True
my_string.count('a') # 2
my_string.find('a') # 3

可以看出,countfind 方法都接受另一个参数。但是,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的函数嵌套的使用方法
Jan 24 Python
使用Python生成url短链接的方法
May 04 Python
分析并输出Python代码依赖的库的实现代码
Aug 09 Python
Python 使用SMTP发送邮件的代码小结
Sep 21 Python
python使用正则表达式的search()函数实现指定位置搜索功能
Nov 10 Python
Python的SimpleHTTPServer模块用处及使用方法简介
Jan 22 Python
Python使用pyautogui模块实现自动化鼠标和键盘操作示例
Sep 04 Python
详解Python可视化神器Yellowbrick使用
Nov 11 Python
根据tensor的名字获取变量的值方式
Jan 04 Python
Python安装whl文件过程图解
Feb 18 Python
Python3利用openpyxl读写Excel文件的方法实例
Feb 03 Python
总结三种用 Python 作为小程序后端的方式
May 02 Python
Python学习笔记之列表和成员运算符及列表相关方法详解
Aug 22 #Python
Django上线部署之IIS的配置方法
Aug 22 #Python
对python中UDP,socket的使用详解
Aug 22 #Python
python3的url编码和解码,自定义gbk、utf-8的例子
Aug 22 #Python
Python学习笔记之集合的概念和简单使用示例
Aug 22 #Python
解决python 3 urllib 没有 urlencode 属性的问题
Aug 22 #Python
python爬虫增加访问量的方法
Aug 22 #Python
You might like
php 字符转义 注意事项
2009/05/27 PHP
PHP函数学习之PHP函数点评
2012/07/05 PHP
PHP实现根据浏览器跳转不同语言页面代码
2013/08/02 PHP
WordPress中用于获取文章信息以及分类链接的函数用法
2015/12/18 PHP
php文件管理基本功能简单操作
2017/01/16 PHP
ThinkPHP5.0框架验证码功能实现方法【基于第三方扩展包】
2019/03/11 PHP
JSON 学习之完全手册 图文
2007/05/29 Javascript
jquery事件的ready()方法使用详解
2015/11/11 Javascript
window.location.reload 刷新使用分析(去对话框)
2015/11/11 Javascript
一些实用性较高的js方法
2016/04/19 Javascript
jQuery实现的可编辑表格完整实例
2016/06/20 Javascript
react.js CMS 删除功能的实现方法
2017/04/17 Javascript
vue-video-player 通过自定义按钮组件实现全屏切换效果【推荐】
2018/08/29 Javascript
iview tabs 顶部导航栏和模块切换栏的示例代码
2019/03/04 Javascript
Vue实现数据请求拦截
2019/10/23 Javascript
[02:53]2018年度DOTA2最佳战队-完美盛典
2018/12/17 DOTA
详解Django中的ifequal和ifnotequal标签使用
2015/07/16 Python
django缓存配置的几种方法详解
2018/07/16 Python
老生常谈python中的重载
2018/11/11 Python
pygame游戏之旅 创建游戏窗口界面
2018/11/20 Python
Python基于plotly模块实现的画图操作示例
2019/01/23 Python
Django框架文件上传与自定义图片上传路径、上传文件名操作分析
2019/05/10 Python
Python传递参数的多种方式(小结)
2019/09/18 Python
Django 允许局域网中的机器访问你的主机操作
2020/05/13 Python
Python如何合并多个字典或映射
2020/07/24 Python
Python Django路径配置实现过程解析
2020/11/05 Python
Python爬虫破解登陆哔哩哔哩的方法
2020/11/17 Python
Python字符串对齐、删除字符串不需要的内容以及格式化打印字符
2021/01/23 Python
css3新单位vw、vh的使用教程
2018/03/23 HTML / CSS
娇韵诗法国官网:Clarins法国
2019/01/29 全球购物
车间操作工岗位职责
2013/12/19 职场文书
厨师长岗位职责
2014/03/02 职场文书
优秀求职信
2014/05/29 职场文书
小学语文继续教育研修日志
2015/11/13 职场文书
vue特效之翻牌动画
2022/04/20 Vue.js
Win11任务栏无法正常显示 资源管理器不停重启的解决方法
2022/07/07 数码科技