Python 基础之字符串string详解及实例

yipeiwu_com7年前Python基础

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 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

Python实现批量将word转html并将html内容发布至网站的方法

本文实例讲述了Python实现批量将word转html并将html内容发布至网站的方法。分享给大家供大家参考。具体实现方法如下: #coding=utf-8 __author__ =...

Python中装饰器高级用法详解

在Python中,装饰器一般用来修饰函数,实现公共功能,达到代码复用的目的。在函数定义前加上@xxxx,然后函数就注入了某些行为,很神奇!然而,这只是语法糖而已。 场景 假设,有一些工作...

wxPython学习之主框架实例

wxPython学习之主框架实例

本文实例讲述了wxPython主框架的简单用法,分享给大家供大家参考。具体如下: 程序代码如下: import wx class MyApp(wx.App): def O...

详解Python函数可变参数定义及其参数传递方式

Python函数可变参数定义及其参数传递方式详解 python中 函数不定参数的定义形式如下 1、 func(*args)  传入的参数为以元组形式存在args...

使用grappelli为django admin后台添加模板

grappelli是github上面star最多的django模板系统 http://django-grappelli.readthedocs.org/en/latest/quickst...