您的位置:首页 > 其它

pyhon 数据类型一

2016-01-21 09:45 274 查看
python中raw_input()输入的是字符,如果需要输入数字,则需要转换一下:

例如:

apple = raw_input('example')
#输入:100
type(apple)
返回<type 'str'>
#需要进行转换:
apple = int(apple)
type(apple)
<type 'int'>     #此时就可以进行数值运算了
</pre><p></p><p>string也可以像list一样进行loop:</p><p></p><pre name="code" class="python">example = [1, 2, 3, 4, 5]
for item in example:
<span style="white-space:pre">	</span>print item<pre name="code" class="python">1
2
3
4
5



</pre><p></p><p><pre name="code" class="python">>>> example = 'apple'
>>> for letter in example:
...     print letter
输出:

a
p
p
l
e


输出string 的子集,或者说是输出string的部分。
例如,我们想输出‘Great Apples’中的Apples,即可这样:

>>> print 'Great Apples'[6:12]
Apples
当我们输出的子集字符是string的后半部分时,且我们不知道字符串的长度(当然可以用len()函数)时我们可以这样:

>>> print 'Great Apples'[6:100]
Apples
或者:

>>> print 'Great Apples'[6:]
Apples


利用lstrip() rstrip() 或者是strip()来去除左、右或者左右whitespace。

string.find(' 字符', '起始点,为int数字')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: