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程序员开发中常犯的10个错误
Jul 07 Python
Linux下Python获取IP地址的代码
Nov 30 Python
状态机的概念和在Python下使用状态机的教程
Apr 11 Python
Python的Bottle框架中返回静态文件和JSON对象的方法
Apr 30 Python
python爬虫入门教程--正则表达式完全指南(五)
May 25 Python
Python MD5加密实例详解
Aug 02 Python
Python+tkinter模拟“记住我”自动登录实例代码
Jan 16 Python
Python安装图文教程 Pycharm安装教程
Mar 27 Python
解决Django后台ManyToManyField显示成Object的问题
Aug 09 Python
简单了解如何封装自己的Python包
Jul 08 Python
Python 使用SFTP和FTP实现对服务器的文件下载功能
Dec 17 Python
java关于string最常出现的面试题整理
Jan 18 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二维数组排序详解
2013/11/06 PHP
使用PHP导出Redis数据到另一个Redis中的代码
2014/03/12 PHP
Thinkphp3.2.3整合phpqrcode生成带logo的二维码
2016/07/21 PHP
php通过执行CutyCapt命令实现网页截图的方法
2016/09/30 PHP
jQuery 常见开发使用技巧总结
2009/12/26 Javascript
6个DIV 135或246间隔一秒轮番显示效果
2010/07/24 Javascript
js创建数据共享接口——简化框架之间相互传值
2011/10/23 Javascript
整理8个很棒的 jQuery 倒计时插件和教程
2011/12/12 Javascript
eval的两组性能测试数据
2012/08/17 Javascript
JS获取图片实际宽高及根据图片大小进行自适应
2013/08/11 Javascript
checkbox全选所涉及到的知识点介绍
2013/12/31 Javascript
jQuery 中DOM 操作详解
2015/01/13 Javascript
表单验证正则表达式实例代码详解
2015/11/09 Javascript
JS获取CSS样式(style/getComputedStyle/currentStyle)
2016/01/19 Javascript
Bootstrap实现下拉菜单效果
2016/04/29 Javascript
详解Vue 动态添加模板的几种方法
2017/04/25 Javascript
除Console.log()外更多的Javascript调试命令
2018/01/24 Javascript
深入剖析Express cookie-parser中间件实现示例
2018/02/01 Javascript
React diff算法的实现示例
2018/04/20 Javascript
Javascript三种字符串连接方式及性能比较
2019/05/28 Javascript
jQuery实现轮播图效果
2019/11/26 jQuery
微信小程序添加插屏广告并设置显示频率(一天一次)
2019/12/06 Javascript
基于Angular 8和Bootstrap 4实现动态主题切换的示例代码
2020/02/11 Javascript
Python操作串口的方法
2015/06/17 Python
python实现多线程抓取知乎用户
2016/12/12 Python
Numpy 中的矩阵求逆实例
2019/08/26 Python
win10子系统python开发环境准备及kenlm和nltk的使用教程
2019/10/14 Python
Python爬虫新手入门之初学lxml库
2020/12/20 Python
浅谈html5之sse服务器发送事件EventSource介绍
2017/08/28 HTML / CSS
菲律宾购物网站:Lazada菲律宾
2018/04/05 全球购物
战友聚会邀请函
2014/01/18 职场文书
小学教师师德反思
2014/02/03 职场文书
2015年小学生暑假总结
2015/07/13 职场文书
2019年励志签名:致拼搏路上的自己
2019/10/11 职场文书
优秀范文:读《红岩》有感3篇
2019/10/14 职场文书
mybatis使用oracle进行添加数据的方法
2021/04/27 Oracle