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 正则式 概述及常用字符
May 07 Python
从零学python系列之教你如何根据图片生成字符画
May 23 Python
在Django中创建动态视图的教程
Jul 15 Python
使用Nginx+uWsgi实现Python的Django框架站点动静分离
Mar 21 Python
Win7下搭建python开发环境图文教程(安装Python、pip、解释器)
May 17 Python
Python基于回溯法子集树模板解决m着色问题示例
Sep 07 Python
学习和使用python的13个理由
Jul 30 Python
在OpenCV里使用特征匹配和单映射变换的代码详解
Oct 23 Python
浅谈pytorch中的BN层的注意事项
Jun 23 Python
利用Python实现Json序列化库的方法步骤
Sep 09 Python
python中spy++的使用超详细教程
Jan 29 Python
Python机器学习之KNN近邻算法
May 14 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 FLEA中二叉树数组的遍历输出
2012/09/26 PHP
Destoon模板制作简明教程
2014/06/20 PHP
深入理解PHP 数组之count 函数
2016/06/13 PHP
php使用PDO获取结果集的方法
2017/02/16 PHP
javascript对数组的常用操作代码 数组方法总汇
2011/01/27 Javascript
一次失败的jQuery优化尝试小结
2011/02/06 Javascript
jQuery操作select的实例代码
2012/06/14 Javascript
javascript中的window.location.search方法简介
2013/09/02 Javascript
JavaScript实现更改网页背景与字体颜色的方法
2015/02/02 Javascript
jquery实现全屏滚动
2015/12/28 Javascript
jQuery获得字体颜色16位码的方法
2016/02/20 Javascript
javascript实现的全国省市县无刷新多级关联菜单效果代码
2016/08/01 Javascript
图片加载完成再执行事件的实例
2017/11/16 Javascript
React Native开发封装Toast与加载Loading组件示例
2018/09/08 Javascript
vue axios基于常见业务场景的二次封装的实现
2018/09/21 Javascript
JavaScript判断浏览器运行环境的详细方法
2019/06/30 Javascript
jQuery与原生JavaScript选择HTML元素集合用法对比分析
2019/11/26 jQuery
VueCli生产环境打包部署跨域失败的解决
2020/11/13 Javascript
[00:35]2016完美“圣”典风云人物:冷冷宣传片
2016/12/08 DOTA
Python实现去除代码前行号的方法
2015/03/10 Python
python登录pop3邮件服务器接收邮件的方法
2015/04/30 Python
Python内置函数OCT详解
2016/11/09 Python
Pandas 同元素多列去重的实例
2018/07/03 Python
浅析Python四种数据类型
2018/09/26 Python
关于python中plt.hist参数的使用详解
2019/11/28 Python
Python跑循环时内存泄露的解决方法
2020/01/13 Python
Python selenium文件上传下载功能代码实例
2020/04/13 Python
PHP如何对用户密码进行加密
2014/07/31 面试题
竞聘演讲稿范文
2014/01/12 职场文书
无偿献血倡议书
2014/04/14 职场文书
安全保证书格式
2015/02/28 职场文书
风雨哈佛路观后感
2015/06/03 职场文书
《穷人》教学反思
2016/02/19 职场文书
Oracle 区块链表创建过程详解
2021/05/15 Oracle
python识别围棋定位棋盘位置
2021/07/26 Python
vue3.0 数字翻牌组件的使用方法详解
2022/04/20 Vue.js