Python字符串输入输出简述
字符串输入
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字符串输入输出简述的更多相关文章
- [转] 强大的python字符串解析
1.python字符串通常有单引号('...').双引号("...").三引号("""...""")或('''...'' ...
- python字符串格式化 %操作符 {}操作符---总结
Python字符串格式化 (%占位操作符) 在许多编程语言中都包含有格式化字符串的功能,比如C和Fortran语言中的格式化输入输出.Python中内置有对字符串进行格式化的操作 %. 模板 格式化字 ...
- 关于python字符串连接的操作
python字符串连接的N种方式 注:本文转自http://www.cnblogs.com/dream397/p/3925436.html 这是一篇不错的文章 故转 python中有很多字符串连接方式 ...
- 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 ...
- Python 字符串
Python访问字符串中的值 Python不支持单字符类型,单字符也在Python也是作为一个字符串使用. Python访问子字符串,可以使用方括号来截取字符串,如下实例: #!/usr/bin/py ...
- python字符串方法的简单使用
学习python字符串方法的使用,对书中列举的每种方法都做一个试用,将结果记录,方便以后查询. (1) s.capitalize() ;功能:返回字符串的的副本,并将首字母大写.使用如下: >& ...
- python字符串基础知识
1.python字符串可以用"aaa",'aaa',"""aaa""这三种方式来表示 2.python中的转义字符串为" ...
- Python 字符串格式化
Python 字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存 一 ...
- Python 字符串操作
Python 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) 去空格及特殊符号 s.strip() .lstrip() .rstrip(',') 复制字符 ...
随机推荐
- CSS3之新UI方案
border-radius 圆角 参数可为像素 也可为百分比 当一个参数时 作用范围为四个角 当两个参数时 作用范围为 左上右下 右上左下 当三个参数时 作用范围为 左上 右上左下 右下 当四个参数时 ...
- Normalize.css的使用及下载
Normalize.css 只是一个很小的CSS文件,但它在默认的HTML元素样式上提供了跨浏览器的高度一致性.相比于传统的CSS reset,Normalize.css是一种现代的.为HTML5准备 ...
- [Erlang 0123] Erlang EPMD
epmd进程和Erlang节点进程如影随形,在Rabbitmq集群,Ejabberd集群,Couchbase集群产品文档中都会有相当多的内容讲epmd,epmd是什么呢? epmd 是Erlan ...
- ZooKeeper:Java客户端网络处理
了解ZooKeeper客户端的实现,对于使用ZooKeeper的客户端非常重要. 通过对客户端源码的阅读,了解了如下信息: 创建ZooKeeper对象时,应会创建一个ClientCnxn(代表了客户端 ...
- 关于Oracle表连接
表连接注意left join on与where的区别: select * from dept; select * from emp; select * from emp a right outer j ...
- 织梦DedeCMS模板防盗的四种方法
织梦(DedeCMS)模板也是一种财富,不想自己辛辛苦苦做的模板被盗用,在互联网上出现一些和自己一模一样的网站,就需要做好模板防盗.本文是No牛收集整理自网络,不过网上的版本都没有提供 Nginx 3 ...
- Solr实战:使用Hue+Solr实现标签查询
公司最近在研究多条件组合查询方案,Google的一位技术专家Sam和我们讨论了几个备选方案. Sam的信: 我做了进一步研究,目前有这么几种做法: 1) 最直接粗暴,只做一个主index,比如按行业+ ...
- zabbix 3.0.3 (nginx)安装过程中的问题排错记录
特殊注明:安装zabbix 2.4.8和2.4.6遇到2个问题,如下:找了很多解决办法,实在无解,只能换版本,尝试换(2.2.2正常 | 3.0.3正常)都正常,最后决定换3.0.3 1.Error ...
- github的使用
1.gitbub概念 github是一个基于git的代码托管平台,付费用户可以建私人仓库,免费用户用公共仓库,但是代码公开. 2.注册账户以及创建仓库 在github官网地址:https://gith ...
- 04.ubuntu下kvm 命令行安装64位ubuntu报"Couldn't find hvm kernel for Ubuntu tree."的问题
1.安装ubuntu时使用的virt-install的配置: virt-install \ --name test4 \ --ram 1024 \ --disk path=/data/01_ubunt ...