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中,最基本的数据结构 ...
随机推荐
- webuploader 小demo
页面写法 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- AHB接口转APB
AHB接口转APB 情景 有一个以AHB接口时序设计的IP,现在需将其移至APB总线上,即将使用APB接口时序驱动该IP. 基本思路 将APB的接口信号映射到AHB的接口信号 要点 APB挂接在AHB ...
- [Recompose] Set the HTML Tag of a Component via a Prop using Recompose
Learn how to user the ‘componentFromProp’ helper and ‘defaultProps’ higher order component to swap t ...
- CGI原理解析之二------WEB服务和CGI交互数据
#include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/wait.h ...
- Bootstrapbutton
Bootstrap 提供了一些选项来定义button的样式,详细例如以下表所看到的: 下面样式可用于<a>, <button>, 或 <input> 元素上: 类 ...
- Android中的动画详解系列【3】——自定义动画研究
在上一篇中我们使用到了位移动画TranslateAnimation,下面我们先来看看TranslateAnimation是如何实现Animation中的抽象方法的: /* * Copyright (C ...
- base64码通过http传输 +号变 空格 问题解决
通过七牛云base64上传图片,通过官方示例上传成功后,根据示例改了一个controller. 通过前端往后端传base64码形式进行测试.死活不通过,七牛报400. 仔细排查后发现,示例转换的bas ...
- 【转载】FormsAuthenticationTicket 对象
1.使用Forms验证存储用户自定义信息 Forms验证在内部的机制为把用户数据加密后保存在一个基于cookie的票据FormsAuthenticationTicket中,因为是经过特殊加密的,所以应 ...
- 代码中jndi数据源的支持
项目中基本都使用Spring框架,支持jndi还是很简单的,只需在spring配置文件中加入 <!-- 使用jndi配置数据源 --> <bean id="dataSour ...
- 【a601】雇佣计划
Time Limit: 1 second Memory Limit: 32 MB [问题描述] 一位管理项目的经理想要确定每个月需要的工人,他知道每月所需的最少工人数.当他雇佣或解雇一个工人时,会有一 ...