熱線電話:13121318867

登錄
首頁精彩閱讀Python 基礎之字符串string詳解及實例
Python 基礎之字符串string詳解及實例
2017-09-05
收藏

Python 基礎之字符串string詳解及實例

Python字符串(string) 詳解及代碼

Python的字符串可以使用單引號('), 雙引號("), 三引號('''); 三引號(''')里面, 可以添加單引號和雙引號, 也可以通過轉義序列(\)添加;
字符串放在一起自動連接成為一個字符串;

字符串前面添加限定詞R或r, 表示是自然字符串(nature string), 可以忽略里面的格式限制;

在物理行末尾添加"\", 可以連接下一個物理行; 括號, 方括號, 大括號也可以一定限度的擴充物理行;

具體參見代碼注釋;

代碼如下:    

# -*- coding: utf-8 -*-
 
#====================
#File: abop.py
#Author: Wendy
#Date: 2013-12-03
#====================
 
#eclipse pydev, python3.3
 
#三引號可以自由的使用雙引號("")和單引號('')
s = ''''' I am a girl and like heels.
Hello, "python" sister. '''
 
#轉義序列"\"
st = '''''Hello, girls, l like (\'''). '''
 
#字符串放在一起自動連接
sa = 'Girls are ''the best. '
 
#r表示自然字符串, 不會進行轉義(\n)
sr = r"nature is good. \n {0}"
 
#"\"表示連接字符串
sc = 'Oh, the lines are too \
large'
 
#括號, 方括號, 大括號, 可以限定范圍, 不用使用轉義
print("the braces can do {thing}.".
   format(thing="lady"))
 
print(s)
print(st)
print(sa)
print(sr)
print(sc)
輸出:
the braces can do lady.
 I am a girl and like heels.
Hello, "python" sister.  
Hello, girls, l like (''').  
Girls are the best.  
nature is good. \n {0}
Oh, the lines are too large
感謝閱讀,希望能幫助到大家。


數據分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數據分析師資訊
更多

OK
客服在線
立即咨詢
日韩人妻系列无码专区视频,先锋高清无码,无码免费视欧非,国精产品一区一区三区无码
客服在線
立即咨詢