分享

python学习笔记(一)

 只怕想不到 2015-09-02

字符串

1.python中字符串连接使用"+"

text1 = 'hello'
text2 = ' world'
print text1+text2     //hello world

2.字符串表示
在python中将值转换为字符串有三种机制
Ⅰ.repr() ,将值以合法形式的Python表达式来表示值;

print 'hello world'   // hello world
print 1000L           // 1000

直接打印出来的结果是不带双引号或者没有类型符的,如果使用repr()

print repr('hello world')     //'hello world'
print repr(1000L)             //1000L

说明使用repr()就能将值转换为python合法形式表达式的字符串。
Ⅱ.str(),将值转换为合理形式的字符串;

print str(1000L)              //1000
print str('hello world')      //hello world

Ⅲ.使用``将值包住也可以实现repr()的效果。
3.input()与raw_input()
input默认输入的值是合法的python表达式。

input('how are you?')
>>>how are you? fine          //name 'fine' id no defined

raw_input会将输入的数据当做原始数据。

raw_input('how are you?')
>>>how are you? fine          // 'fine'

4.长字符串
使用```来代替引导就可以实现长字符串,长字符串可跨多行。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多