Python字符串与转义序列的相关内容的介绍


你了解Python字符串连接的相关典型例子吗?其实Python字符串的连接是很容易学的,只要你看完我们的文章,我们的文章对其有一个详细的比喻与介绍,希望你能有所收获。以下是文章的具体介绍。

生字符串

若要指明字符串中没有转义序列,可以在字符串前加r或R,如r”Newlines are indicated by \n”.

字符串不可改变

有点奇怪哦,和常量没什么区别了)

Python字符串连接

两个字符串放在一起,会被自动的连接起来。如’Whar\’s your ‘‘name?’会自动转化成”What’s your name?”

转义序列

后斜线+字符

  1. \’ ”What’s your name?”=’What\’s your name?”  
  2. \\,\”,\n,\t.  
  3. "This is the first sentence.\  
  4. This is the second sentence."  
  5.  

格式化方法有时我们需要使用其他信息来创建Python字符串。format()就很有用了。

  1. >>> age=25 
  2. >>> name='Swaroop' 
  3. >>> print('{0} is {1} years old'.format(name,age))  
  4. Swaroop is 25 years old  
  5. >>> '{0:.3}'.format(1/3)  
  6. '0.333'  
  7. >>> '{0:_^11}'.format('hello')  
  8. '___hello___'  
  9. >>> '{name} wrote {book}'.format(name='Swaroop'
    ,book='A Byte of Python')  
  10. 'Swaroop wrote A Byte of Python'  
  11. >>>   

上述的内容就是对Python字符串的生字符串与字符串连接以及格式化方法的相关介绍。

编辑推荐】

评论关闭