#!-*- Encodeing: utf-8 -*- #FileName: string.py message="除了数值python还可以通过几种不同的方法操作字符串,用‘’ 或 “ ”" print("=======",除了数值python还可以通过几种不同的方法操作字符串,用‘’ 或 “ ”" ==") print('Hello python') print('I/'m python!') print("I'm python!") print(''' "Hello,python!" she said,''') #字符串分行,在一行末尾添加反斜杠表示延续本行内容 print("======================字符串分行,在一行末尾添加反斜杠表示延续本行内容========") message=" this is muliti lines /n/ this is second line/ ,but not end/ /n/ this line,it's end!" print(message) #有时需要打印换行符 /n,怎么办? #需要在字符串前面加 r print(r"================有时需要打印换行符 /n ,怎么办?========") message=r" print the sign of newline /n" print(message) #打印字符串可用三重引号 '''或者""",所有的格式都包括在字符串中 print(r"=============#打印字符串可用三重引号 ''',可以包含任何符合了========") message=''' Usage: thisgy [Options] -h Display this usage message -H HostName Hostname to connect to //t time for this ''' print(message) #字符串可以用+进行连接,用* 号进行循环 print("=============字符串可以用+进行连接,用* 号进行循环========") message='Hello'+'Python' print(message) #结果:HelloPython message='< '+message*5+' >' print(message)#结果:< HelloPythonHelloPythonHelloPythonHelloPythonHelloPython > #字符串可以用下标查询 print("=============字符串可以用下标查询========") word='Hello,Python' print(word[4]) #结果:o print(word[0:6]) #Hello, print(word[:6]) #Hello, print(word[1:6]) #ello, print(word[1:]) #ello,Python print(word[-1]) #n print(word[-2:]) #on print(word[:-2]) #Hello,Pyth #索引查询不可改变值 #word[0]='W' #切片操作有一个很有用的不变性 print("==========切片操作有一个很有用的不变性: s[:i]+s[i:]=>s ===========") print(word[:3]+word[3:]) print(word[100:]) #结果:空字符串 #print(word[-100]) #IndexError: string index out of range #字符串切片图: # +---+---+---+---+---+ # | H | e | l | p | A | # +---+---+---+---+---+ # 0 1 2 3 4 5 # -5 -4 -3 -2 -1 运行结果: