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

作者:Johnny丶me 时间:2021-07-13 19:13:16 

本文实例讲述了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程序设计有所帮助。

来源:https://blog.csdn.net/Tyro_java/article/details/80714075

标签:Python,字符串
0
投稿

猜你喜欢

  • Python中optparser库用法实例详解

    2023-08-14 01:32:11
  • Python中的集合一起来学习一下

    2022-05-01 05:06:59
  • Asp生成RSS的类_给网站加上RSS

    2011-04-19 11:06:00
  • 使用python+pygame开发消消乐游戏附完整源码

    2021-06-26 03:42:52
  • XML:OpenSearch 浏览器指定搜索应用

    2010-05-04 19:37:00
  • python入门:argparse浅析 nargs='+'作用

    2023-06-26 15:52:58
  • Firebox 3 后退后按钮 diasabled 状态不恢复的一个解决方案

    2008-11-06 12:28:00
  • Django model 中设置联合约束和联合索引的方法

    2023-09-24 09:14:15
  • 抛砖:如何进行互联网项目开发

    2010-01-25 12:25:00
  • java正则表达式之Pattern与Matcher类详解

    2023-06-21 10:14:03
  • 聊聊Python pandas 中loc函数的使用,及跟iloc的区别说明

    2023-08-11 09:11:36
  • python简单实现基数排序算法

    2023-11-10 06:27:27
  • 打开电脑上的QQ的python代码

    2022-08-18 04:21:28
  • Oracle入侵常用操作命令整理

    2009-03-04 11:11:00
  • Python爬虫新手入门之初学lxml库

    2021-11-19 07:16:29
  • jQuery 1.3的VS智能提示下载

    2009-01-18 12:54:00
  • JS中ESModule和commonjs介绍及使用区别

    2023-10-20 22:23:51
  • 解决python彩色螺旋线绘制引发的问题

    2023-06-30 12:49:12
  • Python基于FTP模块实现ftp文件上传操作示例

    2024-01-02 00:04:38
  • asp 去除最后一个逗号为空字符串的代码

    2010-06-09 19:18:00
  • asp之家 网络编程 m.aspxhome.com