1、单引号字符串和转义引用
双引号或单引号字符串里面可以包含另外一种符号,不需要转义,否则用\转义。
2、拼接字符串
挨着写两个字符串会自动连接 【只限于字符串本身,变量不行】
加法运算可以用于拼接字符串
3、字符串表示str和repr
使用str,python会把值转换成合理形式的字符串,以便用户可以理解;
而repr会创建一个字符串,它以合法的python表达式形式来表示值。【区别就在于表达式】
>>> print repr("Hello, world") 'Hello, world' >>> print repr(1000L) 1000L >>> print str("Hello, world") Hello, world >>> print str(1000L) 1000
repr(x)的功能也可以用来实现`X`(反引号),如果希望打印一个包含数字的语句,反引号就比较有用额,但3.0已经不支持了,可以用repr代替
>>> temp = 42 >>> print "The temperature is " + temp Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> print "The temperature is " + temp TypeError: cannot concatenate 'str' and 'int' objects >>> print "The temperature is " + `temp` The temperature is 42
4、input和raw_input对比
input 会【假设】用户输入的事合法的Python表达式。
而raw_input会把所有的输入当做原始数据(raw data),然后将其放入字符串中:
>>> name = input("What is your name?") What is your name?jason Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> name = input("What is your name?") File "<string>", line 1, in <module> NameError: name 'jason' is not defined
>>> input("Enter a number:") Enter a number:3 3 >>> raw_input("Enter a number:") Enter a number:3 '3'
5、长字符串、原始字符串和Unicode
》长字符串:
’‘’ ** ‘’‘ 可以跨行,不需要转义其中引号
》原始字符串:
对于路径的情况,往往需要将所有的\换成\\,这样需要复制之后做很多添加,原始字符串可以解决这个问题
>>> print 'F:\python\npython\Doc' F:\python python\Doc >>> print r'F:\python\npython\Doc' F:\python\npython\Doc》Unicode字符串:
Python中的普通字符串在内部是以8位ASCII码形成存储的。
>>> temp = ['temp1','temp2'] >>> print temp ['temp1', 'temp2'] >>> temp = [u'temp1',u'temp2'] >>> print temp [u'temp1', u'temp2']
相关推荐
Chapter 13: Writing a GUI with Python 209 GUI Programming Toolkits for Python 209 PyGTK Introduction 210 pyGTK Resources 211 Creating GUI Widgets with pyGTK 213 Try It Out: Writing a Simple pyGTK...
《Beginning Python:Using Python 2.6 and Python 3.1》
Beginning Python from Novice to Professional Beginning Python from Novice to Professional
Beginning Python Games Development, Second Edition
Beginning Python Games Development, 2nd Edition will teach you how to create visuals, do event handling, create 3D games, add media elements, and integrate OpenGL into your Python game. In this ...
Updated to reflect the latest in Python programming paradigms and several of the most crucial features found in Python 3, Beginning Python also covers advanced topics such as extending Python and ...
Part I: Dipping Your Toe into Python 1 Chapter 1: Programming Basics and Strings 3 Chapter 2: Numbers and Operators 15 Chapter 3: Variables — Names for Values 31 Part II: Python Language and the ...
beginning python from novice to professional 源代码,python 基础教程
Beginning Python From Novice to Professional(3rd) 英文epub 第3版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Python基础教程 Beginning Python From Novice to Professional.zip,收录了 Python基础教程 Beginning Python From Novice to Professional 中英文各版本和源码,包括(第2版)和 (第3版),都有详尽的书签,不是一级...
Beginning python 3.pdf
Beginning Python From Novice to Professional(3rd) Beginning Python From Novice to Professional(3rd)
Beginning Python Visualization: Crafting Visual Transformation Scripts discusses turning many types of small data sources into useful visual data. And, you will learn Python as part of the bargain....
“开始Python:使用Python 2.6和Python 3.1引入了这种开源的、可移植的、解释的、面向对象的编程语言,它结合了强大的功能和清晰的语法。本书通过讲授基础知识,使您能够快速创建健壮、可靠和可重用的Python应用程序...
Beginning Python Visualization(2nd) 英文无水印pdf 第2版 pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权...
2015年第二版,Beginning Game Development with Python and Pygame 的新版,Beginning Python Games Development, Second Edition 作者: Will McGugan 出版社: Apress 副标题: With PyGame 出版年: 2015-6-21 ...
Beginning Python - From Novice To Professional英文原版PDF,可复制,适合入门python
Beginning Python From Novice to Professional(3rd) 英文无水印pdf 第3版 pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自...
python基础教程 (Beginning Python From Novice to Professional) 书中源码例子
Python 经典英文原版教材,可视化编程教程,适合有一定python编程基础的开发人员使用