第二章、 类型

常量

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 教程 第二章、 类型的更多相关文章

  1. 学习opencv中文版教程——第二章

    学习opencv中文版教程——第二章 所有案例,跑起来~~~然而并没有都跑起来...我只把我能跑的都尽量跑了,毕竟看书还是很生硬,能运行能出结果,才比较好. 越着急,心越慌,越是着急,越要慢,越是陌生 ...

  2. javascript进阶教程第二章对象案例实战

    javascript进阶教程第二章对象案例实战 一.学习任务 通过几个案例练习回顾学过的知识 通过案例练习补充几个之前没有见到或者虽然讲过单是讲的不仔细的知识点. 二.具体实例 温馨提示 面向对象的知 ...

  3. [ABP教程]第二章 图书列表页面

    Web应用程序开发教程 - 第二章: 图书列表页面 关于本教程 在本系列教程中, 你将构建一个名为 Acme.BookStore 的用于管理书籍及其作者列表的基于ABP的应用程序. 它是使用以下技术开 ...

  4. [Python笔记][第二章Python序列-tuple,dict,set]

    2016/1/27学习内容 第二章 Python序列-tuple tuple创建的tips a_tuple=('a',),要这样创建,而不是a_tuple=('a'),后者是一个创建了一个字符 tup ...

  5. [python笔记][第二章Python序列-list]

    2016/1/27学习内容 第二章 Python序列-list list常用操作 list.append(x) list.extend(L) list.insert(index,x) list.rem ...

  6. [Python笔记][第二章Python序列-复杂的数据结构]

    2016/1/27学习内容 第二章 Python序列-复杂的数据结构 堆 import heapq #添加元素进堆 heapq.heappush(heap,n) #小根堆堆顶 heapq.heappo ...

  7. Cobalt Strike系列教程第二章:Beacon详解

    上周更新了Cobalt Strike系列教程第一章:简介与安装,文章发布后,深受大家的喜爱,遂将该系列教程的其他章节与大家分享,提升更多实用技能! 第二章:Beacon详解 一.Beacon命令 大家 ...

  8. [Learn Android Studio 汉化教程]第二章:Android Studio概述(一)

    [Learn Android Studio ]第二章:Android Studio概述(一) Android Studio是一个视窗化的开发环境.为了充分利用有限的屏幕空间,不让你束手束脚,Andro ...

  9. python基础教程-第二章-列表和元组

    本章将引入一个新的概念,:数据结构.数据结构是通过某种方式(例如对元素进行编号)组织在 一起的数据元素的集合,这些数据元素可以是数字或者字符,甚至可以是其他数据结构.在python中,最基本的数据结构 ...

随机推荐

  1. 编译Valgrind arm交叉编译

    1. 下载源码: http://valgrind.org/downloads/valgrind-3.9.0.tar.bz2   2. 加压缩: mkdir sw cd sw tar zxf valgr ...

  2. [Java][web]利用Spring随时随地获得Request和Session

    利用Spring随时随地获得Request和Session 一.准备工作: 在web.xml中加入 <listener> <listener-class> org.spring ...

  3. 账号权限问题导致 masterha_check_repl 检查失败

    在使用 masterha_check_repl --global_conf=/etc/masterha/masterha_default.conf --conf=/etc/masterha/app1. ...

  4. Android 离线缓存的高速实现

    离线缓存是指在有网络的状态下将从server获取的网络数据.如Json 数据缓存到本地,在断网的状态下启动APP时读取本地缓存数据显示在界面上,经常使用的APP(网易新闻.知乎等等)都是支持离线缓存的 ...

  5. [Typescript] What is a Function Type ? Function Types and Interfaces - Are They Related ?

    Function Type: type messageFn = (name: string) => string; function sayHello(name: string): string ...

  6. Java实现的并发任务处理实例

    本文实例讲述了Java实现的并发任务处理方法.分享给大家供大家参考,具体如下: public void init() { super.init(); this.ioThreadPool = new T ...

  7. PatentTips - Scheduling compute kernel workgroups to heterogeneous processors based on historical processor execution times and utilizations

    BACKGROUND OF THE INVENTION  1. Field of the Invention  The present invention relates generally to h ...

  8. KVC设置系统自带属性,不管是不是私有的属性

    KVC   可以设置系统自带属性,不管是不是私有的属性:    1,   2,  

  9. 【9302】&&【a301】兔子繁殖

    Time Limit: 10 second Memory Limit: 2 MB 问题描述 有一种兔子,出生后一个月就可以长大,然后再过一个月一对长大的兔子就可以生育一对小兔子且以后每个月都能生育一对 ...

  10. selenium 爬取空间说说

    package cn.hb.util; import java.io.File; import java.io.FileWriter; import java.io.IOException; impo ...