当前位置: 首页 > article >正文

零基础学Python的第五天||字符串(2)

raw_input和print

自从本课程开始以来,我们还没有感受到computer姑娘的智能。最简单的智能应该体现在哪里呢?想想小孩子刚刚回说话的时候情景吧。

小孩学说话,是一个模仿的过程,孩子周围的人怎么说,她(他)往往就是重复。看官可以忘记自己当初是怎么学说话了吧?就找个小孩子观察一下吧。最好是自己的孩子。如果没有,就要抓紧了。

通过python能不能实现这个简单的功能呢?当然能,要不然python如何横行天下呀。

不过在写这个功能前,要了解两个函数:raw_input和print

这两个都是python的内建函数(built-in function)。关于python的内建函数,下面这个表格都列出来了。所谓内建函数,就是能够在python中直接调用,不需要做其它的操作。

Built-in Functions


|abs() | divmod() | input()| open()| staticmethod()|
|all() | enumerate() | int() | ord() | str()|
|any() | eval() | isinstance()| pow()| sum()|
|basestring() | execfile() | issubclass() | print() | super()|
|bin() | file() | iter()| property()| tuple()|
|bool() | filter() | len() | range() | type()|
|bytearray() | float()| list() | raw_input()| unichr()|
|callable() | format() | locals() | reduce() | unicode()|
|chr() | frozenset() | long() | reload() | vars()|
|classmethod()| getattr()| map() | repr() | xrange()|
|cmp() | globals()| max()| reversed()| zip()|
|compile() |hasattr() | memoryview()| round() | import()|
|complex() |hash() | min()| set() | apply()|
|delattr() |help()| next()| setattr()| buffer()|
|dict() | hex() |object() |slice() | coerce()|
|dir() | id() |oct() |sorted() |intern()|

这些内建函数,怎么才能知道哪个函数怎么用,是干什么用的呢?

不知道你是否还记得我在前面使用过的方法,这里再进行演示,这种方法是学习python的法宝。

>>> help(raw_input)

然后就出现:

Help on built-in function raw_input in module __builtin__:

raw_input(...)
    raw_input([prompt]) -> string

    Read a string from standard input.  The trailing newline is stripped.
    If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
    On Unix, GNU readline is used if enabled.  The prompt string, if given,
    is printed without a trailing newline before reading.

从中是不是已经清晰地看到了raw_input()的使用方法了。

还有第二种方法,那就是到python的官方网站,查看内建函数的说明。2. Built-in Functions — Python 2.7.18 documentation

其实,我上面那个表格,就是在这个网页中抄过来的。

例如,对print()说明如下:

 print(*objects, sep=' ', end='\n', file=sys.stdout)

    Print objects to the stream file, separated by sep and followed by end. sep, end and file, if present, must be given as keyword arguments.

    All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.

    The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. Output buffering is determined by file. Use file.flush() to ensure, for instance, immediate appearance on a screen.

分别在交互模式下,将这个两个函数操练一下。

>>> raw_input("input your name:")
input your name:python
'python'

输入名字之后,就返回了输入的内容。用一个变量可以获得这个返回值。

>>> name = raw_input("input your name:")
input your name:python
>>> name
'python'
>>> type(name)
<type 'str'>

而且,返回的结果是str类型。如果输入的是数字呢?

>>> age = raw_input("How old are you?")
How old are you?10
>>> age
'10'
>>> type(age)
<type 'str'>

返回的结果,仍然是str类型。

再试试print(),看前面对它的说明,是比较复杂的。没关系,我们从简单的开始。在交互模式下操作:

>>> print("hello, world")
hello, world
>>> a = "python"
>>> b = "good"
>>> print a
python
>>> print a,b
python good

比较简单吧。当然,这是没有搞太复杂了。

特别要提醒的是,print()默认是以\n结尾的,所以,会看到每个输出语句之后,输出内容后面自动带上了\n,于是就换行了。

有了以上两个准备,接下来就可以写一个能够“对话”的小程序了。

#!/usr/bin/env python
# coding=utf-8

name = raw_input("What is your name?")
age = raw_input("How old are you?")

print "Your name is:", name
print "You are " + age + " years old."

after_ten = int(age) + 10
print "You will be " + str(after_ten) + " years old after ten years."

对这段小程序中,有几点说明

前面演示了print()的使用,除了打印一个字符串之外,还可以打印字符串拼接结果。

print "You are " + age + " years old."

注意,那个变量age必须是字符串,如最后的那个语句中:

print "You will be " + str(after_ten) + " years old after ten years."

这句话里面,有一个类型转化,将原本是整数型after_ten转化为了str类型。否则,就包括,不信,你可以试试。

同样注意,在after_ten = int(age) + 10中,因为通过raw_input得到的是str类型,当age和10求和的时候,需要先用int()函数进行类型转化,才能和后面的整数10相加。

这个小程序,是有点综合的,基本上把已经学到的东西综合运用了一次。请看官调试一下,如果没有通过,仔细看报错信息,你能够从中获得修改方向的信息。

原始字符串

所谓原始字符串,就是指字符串里面的每个字符都是原始含义,比如反斜杠,不会被看做转义符。如果在一般字符串中,比如

>>> print "I like \npython"
I like 
python

这里的反斜杠就不是“反斜杠”的原始符号含义,而是和后面的n一起表示换行(转义了)。当然,这似乎没有什么太大影响,但有的时候,可能会出现问题,比如打印DOS路径(DOS,有没有搞错,现在还有人用吗?)

>>> dos = "c:\news"
>>> dos
'c:\news'        #这里貌似没有什么问题
>>> print dos    #当用print来打印这个字符串的时候,就出问题了。
c:
ews

如何避免?用前面讲过的转义符可以解决:

>>> dos = "c:\\news"
>>> print dos
c:\news

此外,还有一种方法,如:

>>> dos = r"c:\news"
>>> print dos
c:\news
>>> print r"c:\news\python"
c:\news\python

状如r"c:\news",由r开头引起的字符串,就是原始字符串,在里面放任何字符都表示该字符的原始含义。

这种方法在做网站设置网站目录结构的时候非常有用。使用了原始字符串,就不需要转义了。


http://www.kler.cn/news/157046.html

相关文章:

  • 在python中自己定义一个方法,但是没有写return XXX会有什么影响
  • Liunx系统使用超详细(三)
  • py 启动默认浏览器
  • 工程师业余生活之制作蔬菜盆景
  • 二维码智慧门牌管理系统升级:高效管控门牌贴牌过程
  • JIRA 重建索引加快速度
  • 基于Java SSM车辆租赁管理系统
  • Allegro无法模块复用的解决办法
  • 各种镜像源
  • 使用Serv-U FTP服务器共享文件,实现无公网IP环境下远程访问
  • vm net 方式 静态ip配置访问主机IP和外网
  • pythonGUi不能立即刷新
  • RPG项目01_技能释放
  • Flask Echarts 实现历史图形查询
  • [Swift]RxSwift常见用法详解
  • 联想LJ2655DN激光打印机清零方法
  • Django二转Day03 04
  • Qt应用开发--国产工业开发板全志T113-i的部署教程
  • 基于Spring、SpringMVC、MyBatis的在线云音乐网站
  • Xilinx FPGA——ISE的UCF时序约束
  • LeetCode刷题---打家劫舍问题
  • LoadBalancer将服务暴露到外部实现负载均衡purelb-layer2模式配置介绍
  • 万宾科技可燃气体监测仪科技作用全览
  • SpringBoot自定义异常处理机制
  • GEE:使用拉普拉斯(Laplacian)算子对遥感图像进行卷积操作
  • 英语语法学习 - 每周更新学英语知识点
  • 小米秒享3--非小米电脑
  • 基于YOLOv8深度学习的安全帽目标检测系统【python源码+Pyqt5界面+数据集+训练代码】目标检测、深度学习实战
  • python之ddddocr快速识别
  • 茄子科技张韶全:跨多云大数据平台DataCake在OceanBase的实践