本节内容涉及函数稍多, 需要慢点消化, 一如既往的, 我们不仅说说 python 的最小必要知识, 也讲讲编程英语.

Python内置方法和函数

续接上节课,我们还可以使用Python内置的方法和函数来处理字符串。

upper()函数

upper() 函数, 将文本内的每个字符转换并打印为大写字符.英文:it will convert and print every character inside text in uppercase.

但是首先有个重要的提示:我们使用Python内置方法处理的字符串,不会更改字符串本身的值。But first, a very important note here. The string that we manipulate using the Python built-in >methods does NOT change the value of the string itself.

例子:

text = "Please convert me to all uppercase"

print(text.upper())

# output: PLEASE CONVERT ME TO ALL UPPERCASE

## 函数 print(text.upper()) 它将每一个字符串内的文字转换和打印为大写格式.

print(text)

# output: Please convert me to all uppercase

## 但是,如果我们现在打印 text 这个变量以查看其值,可以看到 text仍然具有原始的小写字符.

由上面这个例子, 可以看出,应用于字符串的方法不会改变字符串的值。

固定变量的大写格式

为了获得更改后的值,需要将其分配给另一个变量,下面例子中 text_new 这个变量存储"将文本更改为所有大写字符"的结果,print(text_new)将打印输出所有的大写字符。

例子:

text = "Please convert me to all uppercase"

text_new = text.upper()

print(text_new)

# output: PLEASE CONVERT ME TO ALL UPPERCASE

lower() 函数

要将字符串转换为小写,可以使用 lower ()方法

例子:

text = "Please convert me to all lowercase" print(text.lower())

# output: please convert me to all lowercase

count() 函数

计算一个或多个字符出现的次数, 可以使用 count()函数的方法英文:To count the number of occurrences of a character or characters, we can use the method count().

例子:

text = "Count number of u's in me"

print(text.count("u"))

# output : 3

输出文本中u这个字符的数量是 3 个.

replace() 函数

要将一个或多个字符替换为其他字符,可以使用replace()的函数方法英文:To replace a character or characters with something else, we can use the method replace()

例子:

text = "Replace this with that"

print(text.replace("this", "that"))

# output : Replace that with that

例子中将原来的 this 替换为 that.

len() 函数

要查找文本的长度,我们可以使用 Python 内置函数 len().英文:To find the length of a text, we can use the Python >built-in function len()

例子:

text = "What is the length of this text"

print(len(text))

# output : 31

这是字符串的长度, 31 ,包含空格

strip() 函数

要删除文本两端的空白,可以使用方法strip().英文:To remove both ends of a text of its whitespaces, we can use the method strip()

例子: text 左右各三个空格,共 21 字符的字符串

text = " Strip both ends "

print(text.strip())

# output: Strip both ends

print(len(text.strip()))

# output: 15

实际输出句子两侧无空格

lstrip() 函数

要删除"前置空格",我们可以使用lstrip()方法.英文:To remove leading whitespaces, we can use the method lstrip().

例子:3个前置空格和3个尾随空格

text = " Strip left end "

print(text.lstrip())

# output : Strip left end

## 将打印“Strip left end空格空格空格”,去掉前置空格,保留尾随空格.

rstrip() 函数

要删除尾随空格,我们可以使用rstrip()方法英文:To remove trailing whitespaces, we can use the method rstrip()

例子:text 字符串包含3个前置空格和3个尾随空格

text = " Strip left end "

print(text.rstrip())

# output : Strip left end

## 将打印“空格空格空格Strip left end”,保留前置空格,去掉尾随空格

is**()函数

以字符 i s 开头的方法, 将返回布尔值 True 或 False。

英文:Methods starting with the characters i s, will return the Boolean value of either True or False. isalnum() checks that EVERY character in the text is an alphanumeric character isalpha() checks that EVERY character in the text is an alphabetic character isdigit() checks that EVERY character in the text is a numeric character isupper() checks that EVERY character in the text is an uppercase character islower() checks that EVERY character in the text is a lowercase character

例子1:

text = "abcdEf"

print(text.isalnum())

# output: True

## 因为每个字符是一个字母数字

print(text.isalpha())

# output: True

## 因为每个字符是一个字母字符

print(text.isdigit())

# output: False

## 因为所有字符都不是数字字符

print(text.isupper())

# output: False

## 因为只有E字符为大写

print(text.islower())

# output: False

## 因为即使所有字符都为小写字母,但E字符仍为大写字母

例子 2:

text = "12345"

print(text.isalnum())

# output: True

## 因为每个字符都是字母数字字符

print(text.isalpha())

# output: False

## 因为所有字符都是数字

print(text•isdigit())

# output: True

## 因为每个字符都是数字字符

print(text.isupper())

# output: False

## 因为所有字符都是数字,所以所有字符都不是大写

print(text.islower())

# output: False

## 因为所有字符都是数字,所以所有字符都不是小写

例子 3:

text = "abc def"

print(text.isalnum())

# output: False

## 因为它包含空格字符,而不是字母数字字符

print(text.isalpha())

# output: False

## 因为它包含空格字符,而不是字母字符

完毕, 待总结。

发布时间: 2020 年 2 月 17 日

当前可任意转载,转载请保存以上信息即可。无需获得授权.

python 变量转字符串_[Python Basic] 字符串处理以及类型转换 2相关推荐

  1. day1 -- Python变量、注释、格式化输出字符串、input、if、while、for

    1.python变量 不需要声明类型,直接 变量名 = 变量值,如 : name = "hahaha" 2.注释: 单行注释,前面加 #,如  # print(info) 多行注释 ...

  2. python的核心数据类型_核心数据类型--字符串

    ## 说明 字符串`字面量`:把文本放入单引号/双引号/三引号中,单双引号混合时使用**3单引号**. python2 使用`unicode`编码,使用u进行标识, 如u'hiyang',python ...

  3. python字符串后面添加字符串_什么是字符串?怎样在Python中添加字符串?

    字符串是一种表示文本的数据类型,字符串中的字符可以是ASCII字符.各种符号以及各种Unicode字符.Python中的字符串有如下三种表现方式. 第1种方式:使用单引号包含字符.示例代码如下: 'a ...

  4. python变量定义大全_详解python变量与数据类型

    这篇文章我们学习 Python 变量与数据类型 变量 变量来源于数学,是计算机语言中能储存计算结果或能表示值抽象概念,变量可以通过变量名访问.在 Python 中 变量命名规定,必须是大小写英文,数字 ...

  5. python实践心得体会_“Python自然语言实践”——总结(一),实战

    正则表达式在NLP中的基本应用 正则表达式的作用: (1)将文档内容从非结构化转为结构化以便后续的文本挖掘 (2)去除"噪声",在处理大量文本片段的时候,有非常多的文字信息与最终输 ...

  6. python整数格式显示_[python之路]格式化显示

    格式化显示 以下整理自 python字符串格式化 *输出结果的空格在md预览中没效果(用代码块三个撇号就可以保留格式了) 一.使用格式化符来格式化字符串: Python支持的所有格式化符: 格式化符 ...

  7. python 变量命名空间_Python命名空间– Python变量范围

    python 变量命名空间 In this tutorial, we are going to learn about Python Namespace and variable scope. In ...

  8. python输出文本居中_#python PIL ImageDraw text 文本居中#

    python pip pil有什么东西 你所问的问题实是属1.先参考[教程]Python中的内置的和方的模块搞懂PIL是属于第三方Python模块2.再参考:[待完善][总结]Python安装第三方的 ...

  9. python shell如何打开_“python shell怎么打开“python shell启动教程

    python shell怎么打开 1.简介:如何在python中运行shell(bash命令) 2.工具/原料:python库:os.py 3.方法:import os command = 'date ...

  10. 减去字符串_从文本字符串中提取指定值的6个超级技巧解读

    在实际的工作中,从指定的字符串中提取指定文本也是常用的技巧之一,除了手动操作之外,下文的8种应用技巧也是必须要掌握的. 一.Left函数法.功能:从指定文本字符串的第一个字符开始,提取指定长度的字符串 ...

最新文章

  1. MPY634U 四象限模拟乘法器
  2. pyqt5动态添加按钮
  3. wxpython实现鼠标拖动事件
  4. PowerBI随笔(7)-lookupvalue、divide
  5. GDCM:获取dicom文件Sequence的长度的测试程序
  6. Exception在语义上的处理。在系统中的意义。
  7. 手语识别_如何使用转移学习进行手语识别
  8. Jsp对字符串的处理
  9. netty时间轮HashedWheelTimer文档翻译及简单说明
  10. 蚂蚁区块链正式升级为蚂蚁链:首次公布“日活”超1亿
  11. 大吉大利,今晚如何用R语言解锁“吃鸡”正确姿势
  12. 实习踩坑之路:实习以来获得的踩坑经验、一些提升开发效率的方法或者常用技巧
  13. html选择时间区间控件,Html5添加用户选择一个日期时间范围的日期选择器插件教程...
  14. 为方便储户,某银行拟开发计算机储蓄系统。
  15. FLAG_ACTIVITY_CLEAR_TOP和singleTask的区别
  16. 永中office linux卸载,ubuntu下完全彻底删除永中office的方法
  17. 同义词,反义词(antonym,synonym)的英文表达理解
  18. 十五款固态硬盘收获季节展示
  19. 流媒体之老黄谈流媒体服务与视频网站研发
  20. 数字unicode码

热门文章

  1. explicit构造函数
  2. ubuntu16.04上安装graphy-easy
  3. P6 计算机系统的层次结构
  4. 数据结构排序、查找算法
  5. C语言中变量的链接属性
  6. 转移指令的原理---汇编学习笔记
  7. C++和Java中类继承同名函数的区分
  8. php 保护变量,保护PHP中的“包含污染”变量
  9. 【以太坊智能合约】Embark Framework 开发基础篇
  10. A quick complete tutorial to save and restore Tensorflow models