python 教程 第二章、 类型
第二章、 类型
常量
5,1.23,9.25e-3,’This is a string’,”It’s a string!”
1) 数
整数:2
长整数:
浮点数:3.23,52.3E-4
复数:-5+4j,2.3-4.6j
ac =-8.33 +1.2j
print ac.real #-8.33
print ac.imag #1.2
print ac.conjugate() #(-8.33-1.2j)
二进制:0b1000
八进制:0o307
十六进制:0xFF
2) 字符串
单引号(')
'Quote me on this'
双引号(")
"What's your name?"
三引号('''或""") 可以在三引号中自由的使用单引号和双引号
'''This is a multi-line string. This is the first line.
This is the second line.
"What's your name?," I asked.
He said "Bond, James Bond."
'''
原始字符串(前缀r或R)raw strings
r"Newlines are indicated by \n"。
Unicode字符串(前缀u或U)
u"This is a 中文 string."
Byte strings(in 3.0)
S = b'spam'
字符串的方法
name = 'Swaroorp' # This is a string object
print name.encode('latin-1') #Swaroorp
print name.startswith('Swa') #True
print name.endswith('spam') #False
print name.find('war') #1
print name.find('warx') #-1
print name.rstrip() #remove whitespace #Swaroorp
print name.isdigit() #False
print name.replace('oo', 'xx') #Swarxxrp
print name.upper() #swaroorp
print name.lower() #swaroop
print name.split('r') #['Swa', 'oo', 'p']
print map(ord, name) #[83, 119, 97, 114, 111, 111, 114, 112]
print map(chr, (map(ord, name))) #['S', 'w', 'a', 'r', 'o', 'o', 'r', 'p']
print '-'.join(name) #S-w-a-r-o-o-r-p
print 'spam' in name #False
for x in name: print(x) #s w a r o o r p)
print [c * 2 for c in name] #['SS', 'ww', 'aa', 'rr', 'oo', 'oo', 'rr', 'pp']
了解这些方法的完整列表,请参见help(str)。
注意:
1.字符串不可变
2.字符串自动连接. 'What\'s' 'your name?'= "What's your name?"。
3.没有Char类型
4.一定要用自然字符串处理正则表达式,否则会需要使用很多的反斜杠
Format方法
类似参数传递
>>> template = '{0}, {1} and {2}' # By position
>>> template.format('spam', 'ham', 'eggs')
'spam, ham and eggs'
>>> template = '{motto}, {pork} and {food}' # By keyword
>>> template.format(motto='spam', pork='ham', food='eggs')
'spam, ham and eggs'
>>> template = '{motto}, {0} and {food}' # By both
>>> template.format('ham', motto='spam', food='eggs')
'spam, ham and eggs'
3) 转义符(\)
'What\'s your name?'
行末的单独一个反斜杠表示字符串在下一行继续
4) 布尔型
True False
"spam" True
"" False
[] False
{} False
1 True
0.0 False
None False
5) 变量
标识符命名
1. 字母或下划线开头.
2. 其他为字母,下划线,数字。
3.大小写敏感。
python 教程 第二章、 类型的更多相关文章
- 学习opencv中文版教程——第二章
学习opencv中文版教程——第二章 所有案例,跑起来~~~然而并没有都跑起来...我只把我能跑的都尽量跑了,毕竟看书还是很生硬,能运行能出结果,才比较好. 越着急,心越慌,越是着急,越要慢,越是陌生 ...
- javascript进阶教程第二章对象案例实战
javascript进阶教程第二章对象案例实战 一.学习任务 通过几个案例练习回顾学过的知识 通过案例练习补充几个之前没有见到或者虽然讲过单是讲的不仔细的知识点. 二.具体实例 温馨提示 面向对象的知 ...
- [ABP教程]第二章 图书列表页面
Web应用程序开发教程 - 第二章: 图书列表页面 关于本教程 在本系列教程中, 你将构建一个名为 Acme.BookStore 的用于管理书籍及其作者列表的基于ABP的应用程序. 它是使用以下技术开 ...
- [Python笔记][第二章Python序列-tuple,dict,set]
2016/1/27学习内容 第二章 Python序列-tuple tuple创建的tips a_tuple=('a',),要这样创建,而不是a_tuple=('a'),后者是一个创建了一个字符 tup ...
- [python笔记][第二章Python序列-list]
2016/1/27学习内容 第二章 Python序列-list list常用操作 list.append(x) list.extend(L) list.insert(index,x) list.rem ...
- [Python笔记][第二章Python序列-复杂的数据结构]
2016/1/27学习内容 第二章 Python序列-复杂的数据结构 堆 import heapq #添加元素进堆 heapq.heappush(heap,n) #小根堆堆顶 heapq.heappo ...
- Cobalt Strike系列教程第二章:Beacon详解
上周更新了Cobalt Strike系列教程第一章:简介与安装,文章发布后,深受大家的喜爱,遂将该系列教程的其他章节与大家分享,提升更多实用技能! 第二章:Beacon详解 一.Beacon命令 大家 ...
- [Learn Android Studio 汉化教程]第二章:Android Studio概述(一)
[Learn Android Studio ]第二章:Android Studio概述(一) Android Studio是一个视窗化的开发环境.为了充分利用有限的屏幕空间,不让你束手束脚,Andro ...
- python基础教程-第二章-列表和元组
本章将引入一个新的概念,:数据结构.数据结构是通过某种方式(例如对元素进行编号)组织在 一起的数据元素的集合,这些数据元素可以是数字或者字符,甚至可以是其他数据结构.在python中,最基本的数据结构 ...
随机推荐
- ORACEL上传BLOB,深度遍历文件夹
// uploadingDlg.cpp : 实现文件// #include "stdafx.h"#include "uploading.h"#include & ...
- Fragment为载体可自己主动布局的CardView(GitHub上写开源项目初体验)
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 开篇废话: 前些天一直在看Android5.0 的Material Desgin,里面新增 ...
- shell脚本if条件总结
原文链接:https://geniuspeng.github.io/2018/03/12/shell-if/ shell编程中,if-then是一种常见的控制流命令,而if的条件判断一般采用内置命令t ...
- VMware虚拟机12安装linux系统详细教程
亲测有效,附图: 工具/原料 VM ware workstation12虚拟机(百度下载) 深度linux镜像ios系统文件 链接:https://pan.baidu.com/s/1RY1Plgru4 ...
- 【45.65%】【codeforces 560B】Gerald is into Art
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- [ExtJS5学习笔记]第九节 Extjs5的mvc与mvvm框架结构简单介绍
本文地址:http://blog.csdn.net/sushengmiyan/article/details/38537431 本文作者:sushengmiyan ------------------ ...
- Android的NDK开发(5)————Android JNI层实现文件的read、write与seek操作
1. 在Android的Java层实现文件的读写操作是非常简单的,可以参看之前写的博文:http://blog.csdn.net/conowen/article/details/7296121 在JN ...
- hdu 1558 Segment set (并查集)
Segment set Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- thinkphp5开发规范(加强复习之前的)
thinkphp5开发规范(加强复习之前的) 一.总结 一句话总结:和类相关的采用驼峰命名法:变量,函数,类,方法,属性采用驼峰命名发:数据库及文件及配置参数是小写字母加下划:常量大写加下划线 1.T ...
- CSS盒子模型中距离的通俗解释
设一个有两个div,一大一小,小的div在大的div里面,而小的div和大div直接的距离就叫外边距,用margin.margin-left.margin-right.margin-top.margi ...