1. """ 可以插入多行文字.

print """
abC
123'
456''" #单引号, 双引号, 也没有关系
"""

2. 使用中文 utf-8编码

#coding=utf-8
#处理中文
x = "中文".decode('utf-8')
y = u"中文"
print len(x) # 结果为2
print len(y) # 结果为2

3. dir(str) #显示str的方法,属性

>>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

4. help(str.find) #显示str.find的帮助文件

help(str.find)
Help on method_descriptor: find(...)
S.find(sub [,start [,end]]) -> int Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end]. Optional
arguments start and end are interpreted as in slice notation. Return -1 on failure.

5. type(), 显示变量的类型

>>> a = open("temp.txt","w")
>>> type(a)
<type 'file'>

6. 占位符  %s, 字符串   %d, 数字

# %s 替换字符串
>>> print "This is a %s" %"test"
This is a test # %s 可以自动转换数字为字符串
>>> print "This is a number %s" %2
This is a number 2 # %s 两个以上的占位符, 需要用() 刮起来
>>> print "There are two string %s and %s" %(2,"Test")
There are two string 2 and Test
>>> print "There are three string %s , %s and %s" %(2,"Test","TEST")
There are three string 2 , Test and TEST # str.format()替换占位符的方法.
>>> print "Format()function, {} {}".format("TEST",2)
Format()function, TEST 2
>>> print "Format()function, {0} {1}".format("TEST",2)
Format()function, TEST 2
>>> print "Format()function, {1} {0}".format("TEST",2)
Format()function, 2 TEST
>>> print "Format()function, {a} {b}".format(b="TEST",a=2)
Format()function, 2 TEST # 用字典的方式, 替换占位符
>>> print "DICT() %(a)s + %(b)s" %{"b":"TEST","a":"A"}
DICT() A + TEST

7. 文件操作

>>> a = open("tmp.txt","w")
>>> a.write("TEST") # 只能写入字符串
>>> a = open("tmp.txt","r")
>>> a.read()
'TEST'
>>> a.read()
'' # 游标已经到了最后, 需要重新设置游标位置
>>> a.seek(0) # 设置游标位置为0
>>> a.read()
'TEST'

8. 文件操作

a = open("temp.txt","w")
a.write("Line1\nLine2\nLine3\nLine4\nLine5\nLine6\n")
a.close()
import linecache
print linecache.getline("temp.txt",1) #打印temp.txt 第一行 a = linecache.getlines("temp.txt")
print a
#返回列表
['Line1\n', 'Line2\n', 'Line3\n', 'Line4\n', 'Line5\n', 'Line6\n']

9. 列表删除

>>> a = range(10)
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> del a[:] #清空列表里的所有元素
>>> a
[]

Python 零碎信息-基础 01的更多相关文章

  1. Python 零碎信息-基础 02

    1. range xrange 的差别 1.1 range 返回列表对象. 1.2 xrange 返回xrange对象  不需要返回列表里面的值, 节省内存. >>> range(1 ...

  2. python极简教程01:基础变量

    测试奇谭,BUG不见. 其实很久之前,就有身边的同事或者网友让我分享一些关于python编程语言的教程,他们同大多数自学编程语言的人一样,无外乎遇到以下这些问题: 网络上的资料过多且良莠不全,不知道如 ...

  3. Shell脚本笔记(一)一些零碎的基础知识

    一些零碎的基础知识 一.认识Shell脚本 一)相关概念 Shell是一种命令解释器,作用是按次序执行(遇到子脚本,先执行子脚本的命令)用户输入的命令和程序. Shell脚本语言是弱类型语言,与其他脚 ...

  4. python网络编程基础(线程与进程、并行与并发、同步与异步、阻塞与非阻塞、CPU密集型与IO密集型)

    python网络编程基础(线程与进程.并行与并发.同步与异步.阻塞与非阻塞.CPU密集型与IO密集型) 目录 线程与进程 并行与并发 同步与异步 阻塞与非阻塞 CPU密集型与IO密集型 线程与进程 进 ...

  5. Python 招聘信息爬取及可视化

    自学python的大四狗发现校招招python的屈指可数,全是C++.Java.PHP,但看了下社招岗位还是有的.于是为了更加确定有多少可能找到工作,就用python写了个爬虫爬取招聘信息,数据处理, ...

  6. 知了课堂 Python Flask零基础 笔记整理

    目录 起步 安装Python2.7: Python虚拟环境介绍与安装: pip安装flask: 认识url: URL详解 web服务器和应用服务器以及web应用框架: Flask 第一个flask程序 ...

  7. Python 面向对象之五 基础拾遗

    Python 面向对象之五 基础拾遗 今天呢,就剩下的面向对象的相关知识进行学习,主要会学习以下几个方面的知识:1.上下文管理协议,2.为类加装饰器 3.元类 一.上下文管理协议 在学习文件操作的时候 ...

  8. 深度学习入门者的Python快速教程 - 基础篇

      5.1 Python简介 本章将介绍Python的最基本语法,以及一些和深度学习还有计算机视觉最相关的基本使用. 5.1.1 Python简史 Python是一门解释型的高级编程语言,特点是简单明 ...

  9. Python开发(一):Python介绍与基础知识

    Python开发(一):Python介绍与基础知识 本次内容 一:Python介绍: 二:Python是一门什么语言 三:Python:安装 四:第一个程序 “Hello world” 五:Pytho ...

随机推荐

  1. 农民工自学java到找到工作的前前后后

    我是一名地地道道的农民工,生活在经济落后的农村,有一个哥哥和一个弟弟,父母都是地道的农民,日出而作,日落而息,我从小到大学习一直很好,从小学到高一都,成绩在全级一直名列前茅,这样我也顺利了考上省的重点 ...

  2. 新系统设置 github 私钥

    1.首先我得重新在git设置一下身份的名字和邮箱(因为当初都忘了设置啥了,因为遇到坑了)进入到需要提交的文件夹底下(因为直接打开git Bash,在没有路径的情况下,根本没!法!改!刚使用git时遇到 ...

  3. player视频.js

    var playStatus = 'pending'; var html_a = '<div class="weui-dialog__bd" id="lly_dia ...

  4. 如何防止index.html首页被篡改

    近期发现公司网站首页文件经常被篡改为indax.php或indax.html,导致网站的功能无法正常使用,百度搜索关键词,在显示结果中点击公司网站,打开后跳转到别的网站上去了,尤其我们在百度做的推广, ...

  5. java语言描述 用抽象类模拟咖啡机的工作

    import java.util.Scanner; class Test { public static void main(String[] args) { coffee per = new cof ...

  6. EEPROM读写学习笔记与I2C总线(二)

    无论任何电子产品都会涉及到数据的产生与数据的保存,这个数据可能并不是用来长久保存,只是在运行程序才会用到,有些数据体量较大对于获取时效性并不太强,各种各样的数据也就有不同的存储载体,这次在EEPROM ...

  7. DevExpress 学习链接

    http://blog.csdn.net/u013816709/article/category/3114039 http://blog.csdn.net/david_520042/article/c ...

  8. DSP5509的ADC实验

    1. 本次使用esay5509开发板,具体做这个板子叫做大道科技. 2. 5509有2个ADC的输入引脚,就是2个采集通道 3. 看下ADC的寄存器 4. 看下代码中怎么引用ADC的寄存器的,这种写法 ...

  9. linux-flock文件锁之实际运用

    vi test.sh #! /bin/bash echo "Hello World" touch test.lock #随便命名 [root@localhost ~]# flock ...

  10. 收集、分析线上日志数据实战——ELK

    本文来自网易云社区 作者:田躲躲 用户行为统计(User Behavior Statistics, UBS)一直是互联网产品中必不可少的环节,也俗称埋点.对于产品经理,运营人员来说,埋点当然是越多,覆 ...