Python字符串與轉(zhuǎn)義序列的相關(guān)內(nèi)容的介紹
你了解Python字符串連接的相關(guān)典型例子嗎?其實Python字符串的連接是很容易學(xué)的,只要你看完我們的文章,我們的文章對其有一個詳細(xì)的比喻與介紹,希望你能有所收獲。以下是文章的具體介紹。
生字符串
若要指明字符串中沒有轉(zhuǎn)義序列,可以在字符串前加r或R,如r”Newlines are indicated by \n”.
字符串不可改變
(有點奇怪哦,和常量沒什么區(qū)別了)
Python字符串連接
兩個字符串放在一起,會被自動的連接起來。如’Whar\’s your ‘‘name?’會自動轉(zhuǎn)化成”What’s your name?”
轉(zhuǎn)義序列
后斜線+字符
- \’ ”What’s your name?”=’What\’s your name?”
- \\,\”,\n,\t.
- "This is the first sentence.\
- This is the second sentence."
格式化方法有時我們需要使用其他信息來創(chuàng)建Python字符串。format()就很有用了。
- >>> age=25
- >>> name='Swaroop'
- >>> print('{0} is {1} years old'.format(name,age))
- Swaroop is 25 years old
- >>> '{0:.3}'.format(1/3)
- '0.333'
- >>> '{0:_^11}'.format('hello')
- '___hello___'
- >>> '{name} wrote {book}'.format(name='Swaroop'
,book='A Byte of Python')- 'Swaroop wrote A Byte of Python'
- >>>
上述的內(nèi)容就是對Python字符串的生字符串與字符串連接以及格式化方法的相關(guān)介紹。
【編輯推薦】