《Beginning Python From Novice to Professional》学习笔记一:String

字符串操作技巧
本文介绍了Python中字符串的基本操作,包括字符串的合并、转换方法str与repr的区别、不同输入方式input与raw_input的应用场景以及长字符串的定义方式。通过本文,读者可以了解如何更高效地处理字符串。
1.两个字符串放在一起会自动合并
"Let's say " '"Hello, world!"' --->'Let/'s say "Hello, world!"'
但是两个字符串变量放在一起中间要加“+”号才会合并

2.str与repr
str会将变量转化成某种用户能够理解的String方式,如
print str(10000L) --->10000
#str(object) -- Converts a value to a string
repr会将变量转化为Python的合法变量
print repr(10000L) --->10000L
另外repr(x)='x'
#repr(object) -- Returns a string representation of a value

3.input与raw_input
name = input("What is your name? ")回车继续
What is your name? Gumby
会出错,因为input会假设你输入的是有效的Python变量,而此处Gumby未定义。改为
What is your name? 'Gumby'则成功
#input(prompt) -- Gets input from the user
name = raw_input("What is your name? ")回车继续
What is your name? Gumby 则不会报错,因为raw_input会认为输入的均为String
#raw_input(prompt) -- Gets input from the user, as a string

4.'''与r与u
'''
长字符串'''       中间可以分行,可以任意用'、"
r前缀为Raw String,     'Hello /nthyU!'会分行,而r'Hello /nthyU!'就忠实于WhatYouSeeIsWhatYouGet,
u前缀为Unicode String,每个字符用16Bit。
Gain a fundamental understanding of Python’s syntax and features with the second edition of Beginning Python, an up–to–date introduction and practical reference. Covering a wide array of Python–related programming topics, including addressing language internals, database integration, network programming, and web services, you’ll be guided by sound development principles. Ten accompanying projects will ensure you can get your hands dirty in no time. Updated to reflect the latest in Python programming paradigms and several of the most crucial features found in the forthcoming Python 3.0 (otherwise known as Python 3000), advanced topics, such as extending Python and packaging/distributing Python applications, are also covered. What you’ll learn * Become a proficient Python programmer by following along with a friendly, practical guide to the language’s key features. * Write code faster by learning how to take advantage of advanced features such as magic methods, exceptions, and abstraction. * Gain insight into modern Python programming paradigms including testing, documentation, packaging, and distribution. * Learn by following along with ten interesting projects, including a P2P file–sharing application, chat client, video game, remote text editor, and more. Complete, downloadable code is provided for each project! Who is this book for? Programmers, novice and otherwise, seeking a comprehensive introduction to the Python programming language. About the Apress Beginning Series The Beginning series from Apress is the right choice to get the information you need to land that crucial entry–level job. These books will teach you a standard and important technology from the ground up because they are explicitly designed to take you fromnovice to professional.” You’ll start your journey by seeing what you need to know—but without needless theory and filler. You’ll build your skill set by learning how to put together real–world projects step by step. So whether your goal is your next career challenge or a new learning opportunity, the Beginning series from Apress will take you there—it is your trusted guide through unfamiliar territory!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值