python2 实例2 字符串

2018-03-12  本文已影响12人  测试进行中

#!/usr/bin/python

#coding=utf8

print "What's your name?"                #双引号包裹单引号

print 'What\'s your name?'               #使用转义符

a=1998

b="free"

print b + str(a)                         #str()把int转换为string

print b + repr(a)                        #repr() 函数,同\反引号

name = raw_input("What is your name?")

age = raw_input("How old are you?")

type(age)                                #

print "Your name is:",name

print "You are"+age +" years old."       # print() 默认是以\n 结尾的

print r"c:\news\python"                  # 在字符串开头加 t ,原始字符串输出

lang="study python"

b=lang[1:]                               #从 1 号到最末尾的字符

c=lang[:]                                #得到所有字符

d=lang[:10]                              #从第一个到 10 号之前的字符

#如果分号的前面或者后面的序号不写,就表示是到第一个(前面的不写)或最末(后面的不写)

str1="abc"

str2="123"

print str1 + "-->" +str2                  # + 连接字符串

print "a" in str1                         # in 包含返回true,否则false

print max(str1)                           #max

print cmp(str1,str2)               

print ord('a')                            #ord  得到字符对应的数

print str1*3                              # * 重复

print len(str1)                           # 字符串有多少字符

print "I like %s" % "python"              #%s

print "I like {}".format("python")        #string.format()

a="I love Python"

print a.split(" ")                        # split 返回list

b="hello "

print "strip:",b.strip()                  # 去空格

a = "This is a Book"

print "a.istitle() ",a.istitle()          #判断每个单词的第一个字母是否为大写

b='www.itdiffer.com'

c=b.split(".")

print c

print "join:",".".join(c)                 # join 拼接list

结果

上一篇下一篇

猜你喜欢

热点阅读