字符串输入

Python用到的输入一般有两种方式,input()raw_input() ,区别是,前者只能输入数字,后者输入的是字符串,使用如下:

In [226]: help(input)

Help on built-in function input in module __builtin__:

input(...)
input([prompt]) -> value Equivalent to eval(raw_input(prompt)). In [228]: input()
d
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-228-25ede6ea20bf> in <module>()
----> 1 input() <string> in <module>() NameError: name 'd' is not defined In [229]: input()
23
Out[229]: 23 In [230]: input("input a num")
input a num444
Out[230]: 444 In [231]: n = input()
23 In [232]: n
Out[232]: 23 In [233]: s = raw_input("input sth.: ")
input sth.: 123 In [234]: s
Out[234]: '123' In [235]: s = raw_input("input sth.: ")
input sth.: sss In [236]: s
Out[236]: 'sss'

字符串输出

输出使用print即可,后边可加变量,也可以直接用"、'和'''来包含字符串,使用示例如下:

  • 正常情况下均可以使用,可以使用一种包含一个字符串,字符串中可以包含另外一种(但是不可以包含同一种,否则需要转义)
In [241]: print "i'm Tom"
i'm Tom In [242]: print 'abc'
abc In [243]: print "abc"
abc In [244]: print '''abc'''
abc In [245]: print '"hhh"'
"hhh" In [246]: print "'hello world'"
'hello world' In [247]: print 'i'am bt'
File "<ipython-input-247-efa01090d6c6>", line 1
print 'i'am bt'
^
SyntaxError: invalid syntax # 字符串转义
In [248]: print 'i\'m bt'
i'm bt In [249]: print ''' i'm tom, "hhhe"'''
i'm tom, "hhhe"
  • 换行
In [250]: print ''' i
.....: am tom
.....: hhha '''
i
am tom
hhha In [254]: print 'i am \
.....: tom \
.....: hh'
i am tom hh In [255]: print " i\
.....: am \n \
.....: tom \n "
iam
tom
# 此处有空行 # 输出非转义字符串
In [256]: print r"I\'m Tom"
I\'m Tom

数字字符串转换

直接使用str()或者int()即可,没什么可说的,如下:

In [256]: print r"I\'m Tom"
I\'m Tom In [257]: n = raw_input()
123 In [258]: n
Out[258]: '123' In [259]: n = int(n) In [260]: n
Out[260]: 123 In [261]: str(n)
Out[261]: '123'

Python字符串输入输出简述的更多相关文章

  1. [转] 强大的python字符串解析

    1.python字符串通常有单引号('...').双引号("...").三引号("""...""")或('''...'' ...

  2. python字符串格式化 %操作符 {}操作符---总结

    Python字符串格式化 (%占位操作符) 在许多编程语言中都包含有格式化字符串的功能,比如C和Fortran语言中的格式化输入输出.Python中内置有对字符串进行格式化的操作 %. 模板 格式化字 ...

  3. 关于python字符串连接的操作

    python字符串连接的N种方式 注:本文转自http://www.cnblogs.com/dream397/p/3925436.html 这是一篇不错的文章 故转 python中有很多字符串连接方式 ...

  4. StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?

    StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...

  5. Python 字符串

    Python访问字符串中的值 Python不支持单字符类型,单字符也在Python也是作为一个字符串使用. Python访问子字符串,可以使用方括号来截取字符串,如下实例: #!/usr/bin/py ...

  6. python字符串方法的简单使用

    学习python字符串方法的使用,对书中列举的每种方法都做一个试用,将结果记录,方便以后查询. (1) s.capitalize() ;功能:返回字符串的的副本,并将首字母大写.使用如下: >& ...

  7. python字符串基础知识

    1.python字符串可以用"aaa",'aaa',"""aaa""这三种方式来表示 2.python中的转义字符串为" ...

  8. Python 字符串格式化

    Python 字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存 一 ...

  9. Python 字符串操作

    Python 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) 去空格及特殊符号 s.strip() .lstrip() .rstrip(',') 复制字符 ...

随机推荐

  1. InfoPath错误,此文档库已被重命名或删除

    在使用InfoPath发布表单,发布到SharePoint服务器报错,如下介绍: 环境:Windows 2012 DateCenter + Sql 2012 + SharePoint 2013 + O ...

  2. Android 5.X新特性之为RecyclerView添加HeaderView和FooterView

    上一节我们讲到了 Android 5.X新特性之RecyclerView基本解析及无限复用 相信大家也应该熟悉了RecyclerView的基本使用,这一节我们来学习下,为RecyclerView添加H ...

  3. NFR

    你NFR了吗? NFR,即非功能性需求 (Non -Functional Requirements) ,即系统能够完成所期望的工作的性能与质量.具体包括如下内容: – 效率: 软件实现其功能所需要的计 ...

  4. Mac 开发者常用的工具

    转载:http://www.oschina.net/news/53946/mac-dev-tools 在写 Mac 程序员的十个武器之前,我决定先讲一个故事,关于 Mac 和爱情的.(你们不是问 Ma ...

  5. 关于case语句中声明变量并初始化的注意事项

    今天看到一句对这个问题特别精辟的总结,记录如下: It is possible to transfer into a block, but not in a way that bypasses dec ...

  6. 原创:jar的名字可能影响maven的依赖下载

    自己定义了phoenix4.6-client的依赖关系POM,如下: <project xmlns="http://maven.apache.org/POM/4.0.0" x ...

  7. [MySQL Reference Manual]15. 其他存储引擎

    15. 其他存储引擎 15. 其他存储引擎 15.1 设置存储引擎 15.2 MyISAM存储引擎 15.2.1 MyISAM启动选项 15.2.2 Key的空间要求 15.2.3 MyISAM表存储 ...

  8. Linux下命令行安装weblogic10.3.6

    Linux下命令行安装weblogic10.3.6 一.安装前准备工作: 1.创建用户useradd weblogic;创建用户成功linux系统会自动创建一个和用户名相同的分组,并将该用户分到改组中 ...

  9. 3-udev

    Udev 这个是挂载上的u盘 拔掉再插上 查看u盘设备信息 拔掉再插上,显示了 来自为知笔记(Wiz) 附件列表

  10. mongodb高级应用

    一.  高级查询 查询操作符 条件操作符:db.collection.find({"field":{$gt/$lt/$gte/$lte/$eq/$ne:value}}); 匹配所有 ...