【原创】Python第二章——字符串
字符串是一个字符序列,(提醒:序列是Python的一个重要的关键词),其中存放UNICODE字符。Python中的字符串是不可变的(immutable),即对字符串执行操作时,总是产生一个新的字符串而不是修改现有的字符串。
#单引号
A = 'Python'
#引号
B = "Python"
#三引号
C = """Python"""
2. 为什么要这么麻烦?
#单引号不用转义
A = "I'm a Python newcomer"
#引号不用转义
B = 'I have learnt "C" language'
(2)三引号可以让输出的字符串直接跨行
A = "I'm \n\
a newcomer"
如果使用三引号,可以不用使用\n\
B = """I'm a
newcomer"""
#建议
A = ("I'm "
"a newcomer")
#不建议
B = "I'm " + \
"a newcomer"
C = "I'm \
a newcomer"
| 转义字符 | 含义 |
| \newline | 忽略换行 |
| \\ | \ |
| \' | ' |
| \" | " |
| \a | ASCII蜂鸣 |
| \b | ASCII退格 |
| \f | ASCII走纸 |
| \r | 回车CR |
| \n | 换行LF |
| \t | ASCII制表符 |
| \v | ASCII垂直指标 |
| \ooo | 给定八进制字符 |
| \xhh | 给定8位十六进制字符 |
| \uhhhhh | 给定16位十六进制字符 |
| \Uhhhh hhhh | 给定32位十六进制字符 |
| \N{name} | 给定名称的Unicode字符,name是一个标准Unicode名称 |
import unicodedata
title = u"Klüft skräms inför på fédéral électoral große"
B = unicodedata.normalize('NFKD', title).encode('ascii','ignore')
print(B)
(2)字符排序问题
format % values
print('number is %d'%10)
将输出number is 10
其中的%d就是一个转换标识符,表示这里是一个整数,%d被替换成真实值10。
转换标志有:
| Flag | Meaning |
|---|---|
| '#' | 转换标志的起始符 |
| '0' | 用0在数的字段宽度内进行填充 |
| '-' | 左对齐 (如果给出了上面的0转换和左对齐转换,则忽略0转换 |
| ' ' | (一个空格)在一个有符号转换中,如果是一个正数,那么在开头加上空格而不是'+' |
| '+' | 若数为正数,则在开头添加'+'(忽略空格标志). |
| 格式字符 | 含义 |
|---|---|
| dioux | 精度表示最少显示数字位数,如果精度过大则以0填充,否则原样输出 |
| ef | 精度表示小数点后的数字位数 |
| g | 精度表示最大有效位 |
| s | 精度表示最大的字符数 |
The conversion types are:
| Conversion | Meaning | Notes |
|---|---|---|
| 'd' | Signed integer decimal. | |
| 'i' | Signed integer decimal. | |
| 'o' | Signed octal value. | (1) |
| 'u' | 已废弃,等同于d | (7) |
| 'x' | Signed hexadecimal (lowercase). | (2) |
| 'X' | Signed hexadecimal (uppercase). | (2) |
| 'e' | Floating point exponential format (lowercase). | (3) |
| 'E' | Floating point exponential format (uppercase). | (3) |
| 'f' | Floating point decimal format. | (3) |
| 'F' | Floating point decimal format. | (3) |
| 'g' | Floating point format. Uses lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. | (4) |
| 'G' | Floating point format. Uses uppercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. | (4) |
| 'c' | Single character (accepts integer or single character string). | |
| 'r' | String (converts any Python object using repr()). | (5) |
| 's' | String (converts any Python object using str()). | (5) |
| 'a' | String (converts any Python object using ascii()). | (5) |
| '%' | 输出% |
Notes:
The alternate form causes a leading zero ('0') to be inserted between left-hand padding and the formatting of the number if the leading character of the result is not already a zero.当使用了转换标志0,那么填充0的时候,填充的位置将是0o和数字之间,比如%#08o' % 34的结果是0o000042
The alternate form causes a leading '0x' or '0X' (depending on whether the 'x' or 'X' format was used) to be inserted between left-hand padding and the formatting of the number if the leading character of the result is not already a zero.当使用了转换标志0,那么填充0的时候,填充的位置将是0x和数字之间,比如%#08x' % 34的结果是0x000022。
The alternate form causes the result to always contain a decimal point, even if no digits follow it.
The precision determines the number of digits after the decimal point and defaults to 6.精度控制决定了小数点后的位数,默认是6位
The alternate form causes the result to always contain a decimal point, and trailing zeroes are not removed as they would otherwise be.
The precision determines the number of significant digits before and after the decimal point and defaults to 6.
If precision is N, the output is truncated to N characters.如果上面指定的精度是N,那么字符串的最大长度是N
- See PEP 237.
Since Python strings have an explicit length, %s conversions do not assume that '\0' is the end of the string.
因为Python的String有显式的长度,所以%s转换不认为'\0'是字符串的结尾
Changed in version 3.1: %f conversions for numbers whose absolute value is over 1e50 are no longer replaced by %g conversions.
【原创】Python第二章——字符串的更多相关文章
- 简学Python第二章__巧学数据结构文件操作
#cnblogs_post_body h2 { background: linear-gradient(to bottom, #18c0ff 0%,#0c7eff 100%); color: #fff ...
- Python 第二章-列表和元组
第二章-列表和元组 2.0 在Python中,最基本的数据结构是序列(sequence).序列中的每个元素被分配一个序列号-即元素的位置, 也称为索引.第一个索引是0,第二个是1,以此类推. ...
- python第二章:数据类型--小白博客
标准数据类型 Python3 中有六个标准的数据类型: Number(数字) String(字符串) List(列表) Tuple(元组) Set(集合) Dictionary(字典) Python3 ...
- Python第二章-变量和数据类型
变量和数据类型 一.什么是变量,常量 思考:程序执行指的是什么? 对数据进行存储处理和计算,最终获得结果,这是程序执行的本质. 变量的概念和在数学中的变量的概念一样的,只是在计算机程序中,变量不仅可以 ...
- 【原创】Python第二章——行与缩进
Python的基本组成——逻辑行和缩进 a="我是一个物理行" a="""我是一个逻辑行 因为我一条语句便跨越了2个物理行""&q ...
- 【原创】Python第二章——标识符命名规则
在Python中,一切都是对象,包括常量数据类型,如整数数据类型(1,2,3...),字符串数据类型("ABC").想要使用这些对象,就要使用它的对象引用.赋值操作符,实际上是使得 ...
- python第二章:控制流
变成实际上是一个过程的提现,每个过程都是有多个流程块组成. 比如:判断是否下雨的过程 1.布尔值 在第一章最后举例了比较操作后,最终返回的结果 True or False True 和 False是一 ...
- machine learn in python 第二章2.1.1
1大约 sklearn.datasets from sklearn.datasets import load_iris import numpy as np data = load_iris() da ...
- python 第二章 对象与类型
可变对象和不可变对象 1,可变对象,list(列表),dict(字典),集合(set),字节数组. 2,不可变对象,数值类型,字符串,字节串,元组(具体形式 ()). 注意条件:可变和不可变指的是该对 ...
随机推荐
- 移动端各种滚动场景需求的插件better-scroll
移动端各种滚动场景需求的插件: 文档地址: better-scroll:https://ustbhuangyi.github.io/better-scroll/doc/zh-hans/#better- ...
- C语言的数据类型的本质和提高学习
一.数据类型的概念 类型是对数据的抽象 类型是相同的数据有相同的表示形式.存储格式以及相关的操作 程序中使用的数据必定属于某一种数据类型 1.算术类型: 包括三种类型:整数类型.浮点类型,枚举型. ...
- loadrunner整体压测执行操作步骤
lr11安装包链接:https://pan.baidu.com/s/1hF3j2Vi_xB8BhT70P1ZdBg 提取码:n3zn lr12安装包链接:https://pan.baidu.com/s ...
- idea安装了Mybaits Plugin插件后,启动不起来了
之前安装了一些插件,谁知道重启完了之后,直接启动不起来了,报错信息如下: cannot load project fatal error initializing plugin com.seven7. ...
- Batch_Size对网络训练结果的影响
最近在跑一些网络时发现,训练完的网络在测试集上的效果总是会受Batch_Size 大小的影响.这种现象跟以往自己所想象的有些出入,于是出于好奇,各种搜博客,大致得出了自己想要的答案,现写一篇博客记录一 ...
- JAVA执行远端服务器的脚本
JAVA执行远端服务器的脚本 问题描述 实现思路 技术要点 代码实现 问题描述 工作中遇到这样一个问题,我们的应用为了实现高可用会采取双机部署,拓扑图大致如下: 这种方案可以简单的保证高可用,即便应用 ...
- Python中使用cx_Oracle调用Oracle存储过程
import cx_Oracle as cx import datetime def execute_sql(): # 声明变量 date_time = datetime.datetime.now() ...
- String StringBuilder StringBuffer区别
String StringBuilder StringBuffer String类是final类,不可以被继承,且它的成员方法也是final方法,当一个字符串对象进行操作操作时,任何的改变不会影响到这 ...
- redis设置远程通过密码进行连接
个人配置:服务器+本地鸡+win 文件概况.↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ 首先修改第一个redis.windows.conf 56行左右,将bind 127.0.0.1注释掉(行前面加上# ...
- Android中屏幕保持唤醒
1.锁的类型 PowerManager中各种锁的类型对CPU .屏幕.键盘的影响: PARTIAL_WAKE_LOCK : 保持CPU 运转,屏幕和键盘灯有可能是关闭的. SCREEN_DIM_WAK ...