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中的文本处理
Apr 11 Python
flask + pymysql操作Mysql数据库的实例
Nov 13 Python
python如何让类支持比较运算
Mar 20 Python
Python cookbook(数据结构与算法)同时对数据做转换和换算处理操作示例
Mar 23 Python
使用11行Python代码盗取了室友的U盘内容
Oct 23 Python
PyQt弹出式对话框的常用方法及标准按钮类型
Feb 27 Python
使用python打印十行杨辉三角过程详解
Jul 10 Python
在vscode中配置python环境过程解析
Sep 28 Python
jupyter notebook中美观显示矩阵实例
Apr 17 Python
python输出数学符号实例
May 11 Python
Python基于smtplib模块发送邮件代码实例
May 29 Python
Python3利用openpyxl读写Excel文件的方法实例
Feb 03 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
海河写的 Discuz论坛帖子调用js的php代码
2007/08/23 PHP
PHP读取网页文件内容的实现代码(fopen,curl等)
2011/06/23 PHP
一个PHP实现的轻量级简单爬虫
2015/07/08 PHP
配置Nginx+PHP的正确思路与过程
2016/05/10 PHP
PHP7新增函数
2021/03/09 PHP
有效的捕获JavaScript焦点的方法小结
2009/10/08 Javascript
js如何取消事件冒泡
2013/09/23 Javascript
nodejs开发环境配置与使用
2014/11/17 NodeJs
jQuery的context属性用法实例
2014/12/27 Javascript
jQuery表单域属性过滤器用法分析
2015/02/10 Javascript
javascript中一些util方法汇总
2015/06/10 Javascript
JavaScript学习笔记之取数组中最大值和最小值
2016/03/23 Javascript
修改jquery中dialog的title属性方法(推荐)
2016/08/26 Javascript
jQuery DateTimePicker 日期和时间插件示例
2017/01/22 Javascript
Vue自定义指令拖拽功能示例
2017/02/17 Javascript
Javascript中八种遍历方法的执行速度深度对比
2017/04/25 Javascript
记一次webapck4 配置文件无效的解决历程
2018/09/19 Javascript
vue 的 solt 子组件过滤过程解析
2019/09/07 Javascript
JavaScript如何判断input数据类型
2020/02/06 Javascript
[59:00]DOTA2-DPC中国联赛 正赛 Ehome vs PSG.LGD BO3 第一场 3月7日
2021/03/11 DOTA
在Python中操作字典之setdefault()方法的使用
2015/05/21 Python
python正则表达式re之compile函数解析
2017/10/25 Python
三个python爬虫项目实例代码
2019/12/28 Python
详解Css3新特性应用之过渡与动画
2017/01/10 HTML / CSS
世界上最大的艺术和工艺用品商店:MisterArt.com
2018/07/13 全球购物
工程项目经理岗位职责
2013/12/15 职场文书
高中考试作弊检讨书
2014/01/14 职场文书
电厂职工自我鉴定
2014/02/20 职场文书
邮政竞聘演讲稿
2014/09/03 职场文书
司法局群众路线教育实践活动整改措施
2014/09/17 职场文书
高校师德师风自我剖析材料
2014/09/29 职场文书
高考作弊检讨书1500字
2015/02/16 职场文书
2015年学校食堂工作总结
2015/04/22 职场文书
多表查询、事务、DCL
2021/04/05 MySQL
go语言中fallthrough的用法说明
2021/05/06 Golang
面试提问mysql一张表到底能存多少数据
2022/03/13 MySQL