python3知识点之---------字符串的介绍

1. 定义

    其实字符串就是一系列字符,用引号括起来的就是字符串,其中的引号可以是单引号或者双引号。

      比如 "This is a string"   'This is a string'

 

 

2. 访问字符串中的字符

    这里的访问跟我上篇列表的形式一样,可以通过索引和切片的形式访问字符

1 #!/usr/bin/env python
2 # encoding: utf-8
3 
4 str = "hello, world!"
5 
6 print(str[2])
7 print(str[2:])
View Code

    结果为:

l
llo, world!

    如果我想重复字符串的话,可以按一下操作:

1 #!/usr/bin/env python
2 # encoding: utf-8
3 
4 str = "hello, world!"
5 
6 print(str*4)
View Code

    结果为:

hello, world!hello, world!hello, world!hello, world!

 

    关键字in (not in) 判断是否在其中,并返回布尔值

1 #!/usr/bin/env python
2 # encoding: utf-8
3 
4 str = "hello, world!"
5 
6 print("ello" in str)
7 print("123" in str)
View Code

    结果为:

True
False

 

 

3. 字符串的拼接输出:

    ① 通过加号+的形式拼接,    比如 a = "hello"  b = "word!"   print(a +" , " + b)   结果为:"hello, world!"     缺点就是耗内存太大,相当于新找一块存放hello, world!的内存。

    ② 通过逗号,的形式拼接,      比如 a = "hello"  b = "word!"   print(a, b)   结果为:"hello world!"

    ③ 通过内置方法join()拼接,   比如 a = "hello"  b = "word!"   print("-".join(a))   结果为:"h-e-l-l-o"

    常用的是后两个,第一个最好不要用。

1 #!/usr/bin/env python
2 # encoding: utf-8
3 
4 a = "hello"
5 b = "world!"
6 
7 print("str1 = " + a + " " + b )
8 print("str2 =", a, b)
9 print(' '.join(["str3 =",a,b]))
View Code

    结果为:

str1 = hello world!
str2 = hello world!
str3 = hello world!

 

 

4. 字符串的格式化输出:

    Python3中内置有对字符串进行格式化的操作%。例子如下:

 1 #!/usr/bin/env python
 2 # encoding: utf-8
 3 
 4 name = input("please enter your name: ")
 5 age = int(input("Age: "))
 6 job = input("Job: ")
 7 salary = int(input("Salary: "))
 8 
 9 msg = '''------info of %s------
10          Name: %s
11          Age: %d
12          Job: %s
13          Salary: %f
14          Your will be retired in %d years
15          ---------end----------
16       ''' % (name, name, age, job, salary, 65-age)
17 
18 
19 print(msg)
View Code

    结果为:

please enter your name: Hyan
Age: 24
Job: Student
Salary: 23500
------info of Hyan------
         Name: Hyan
         Age: 24
         Job: Student
         Salary: 23500.000000
         Your will be retired in 41 years
         ---------end----------

 

    下面是字符串格式化符号:

符   号描    述
         %c  格式化字符及其ASCII码
         %s  格式化字符串
          %d  格式化整数
                    %u   格式化无符号整型
                    %o  格式化无符号八进制数
                    %x  格式化无符号十六进制数
                    %X  格式化无符号十六进制数(大写)
                    %f  格式化浮点数字,可指定小数点后的精度
                    %e  用科学计数法格式化浮点数
                    %E  作用同%e,用科学计数法格式化浮点数
                    %g  %f和%e的简写
                    %G  %f 和 %E 的简写
                     %p  用十六进制数格式化变量的地址

     

      格式化操作符指令: 

符号功能
* 定义宽度或者小数点精度
- 用做左对齐
+ 在正数前面显示加号( + )
<sp> 在正数前面显示空格
# 在八进制数前面显示零('0'),在十六进制前面显示'0x'或者'0X'(取决于用的是'x'还是'X')
0 显示的数字前面填充'0'而不是默认的空格
% '%%'输出一个单一的'%'
(var) 映射变量(字典参数)
m.n. m 是显示的最小总宽度,n 是小数点后的位数(如果可用的话)

    

       在需要在字符中使用特殊字符时,python用反斜杠(\)转义字符。如下表:

转义字符描述
\(在行尾时) 续行符
\\ 反斜杠符号
\' 单引号
\" 双引号
\a 响铃
\b 退格(Backspace)
\e 转义
\000
\n 换行
\v 纵向制表符
\t 横向制表符
\r 回车
\f 换页
\oyy 八进制数,yy代表的字符,例如:\o12代表换行
\xyy 十六进制数,yy代表的字符,例如:\x0a代表换行
\other 其它的字符以普通格式输出

 

 

5. 字符串的内置方法的介绍:

  由于内置的方法太多,我这里就简单的介绍一下常用的方法。

    常用的方法:

        str = "hello world!"

        str.count('o', start= 0,end=len(string)) #查找o在字符串中出现的次数。后面beg=0意思是从索引0开始查找,不写默认为0,同样end默认为字符串长度。

        str.center(50, '#')  #返回一个制定宽度的居中的字符串,第一个参数是指宽度,第二个参数是指填充物用#代替。

        str.startswith('he') #判断该字符串是否以he开头。

        str.find('o', beg=0, end=len(string)) #查找o在字符串中第几个位置,返回值为索引。后面beg=0意思是从索引0开始查找,不写默认为0,同样end默认为字符串长度。这里强调的是如果找不到返回值为-1。

        str.index('o', beg=0, end=len(string)) # 跟上面的find方法基本上一样,唯一的差别在于找不到的情况下,不会返回-1,而是直接报错。

        str.rfind('o', beg=0 end=len(string)) #跟上面的find方法差不多,唯一的差别就是从右开始查找。

        str.lower() #将字符串全部转化为小写。

        str.upper() #将字符串全部转化为大写。

        str.title() #所有的首字母大写。

        str.strip() #消除字符串前后的空格。

        str.lstrip() #消除字符串的开头的空格。

        str.rstrip() #消除字符串的结尾的空格。

        str.replace('world', 'hyan', 1)  #将字符串中world替换为hyan,参数1的意思是替换第一次出现的,2就是替换前两个。

        str.split(str="", num=string.count(str)) #将字符串进行切片,并返回一个列表。第一个参数就是分隔符,默认为空格,第二个参数是就是分割的次数。

        str.rsplit(str="", num=string.count(str)) #跟split一样,不过是从倒着切割的。

        str.isdigit() #如果字符串只包含数字,返回True,否则返回Flase

        str.format(mapping) #字符串格式化输出的另一种格式

        我们在这里具体介绍一下format方法的一些用法

1 #!/usr/bin/env python
2 # encoding: utf-8
3 
4 print("my name is {}, and the age is {}".format('hyan', '23'))
5 print("my name is {1}, and the age is {0}".format('23', 'hyan'))
6 print("my name is {name}, and the age is {0} and the salary is {1}".format('23', '23500', name='hyan'))
View Code

    结果为:

my name is hyan, and the age is 23
my name is hyan, and the age is 23
my name is hyan, and the age is 23 and the salary is 23500

         从上面的例子也可以看出来,参数的传递是跟位置有关,默认是按着顺序一对一传递,也可以通过数字方式传递,也可以用关键字的方式传递。数字和关键字可以混合传递。          

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

        

         

 

posted @ 2018-01-18 17:16  落叶心声  阅读(193)  评论(0编辑  收藏  举报

Contact with me