1.

       len函数可以直接测得字符串的长度,使用语法如下:


[python] view plain copy

1. size = len(string)


举个小例子吧


[python] view plain copy

1. str1 = 'www.jeapedu.com'
2. size = len(str1)  
3. index = 0
4. while
5. print
6. 1
7. print




python 字符包含 python包含字符串函数_字符串


      2.字符串连接: +运算

      在python里通常将两个字串串连接通过“+”操作运算将两个字符串连接在一起。


[python] view plain copy

1. str1 = 'www.'
2. str2 = 'jeapedu.'
3. str3 = 'com'
4.   
5. str4 = str1 + str2 + str3  
6. print
7. str5 = str1,str2,str3  
8. print

      用+和用逗号连接多个字符串的结果是不一样的,读者可以看前述内容分析一下。

python 字符包含 python包含字符串函数_bc_02


       3.字符串in运算和not in 运算

        语法结构:


[python] view plain copy

1. str1 in

例程如下:

[python] view plain copy

1. str1 = 'jeapedu'
2. str2 = 'www.jeapedu.com'
3. if str1 in
4. print


和in的用法一样,这里我们就不再举例子了。

     4. 其他不太常用的函数

     4.1 判断类型的字符串函数

1.1 判断是否都是有效字符字符串isalnum() 

num函数的作用是判断字符串里是否都是由字符、数字等常见有效字符的字符串。

    


[python] view plain copy

1. str1 = 'a1b2c3d4'
2. str2 = ',./;]'
3. str3 = ',a./;]'
4. str4 = ',6./;]'
5. t = str1.isalnum()  
6. print
7. f = str2.isalnum()  
8. print
9. t = str3.isalnum()  
10. print
11. f = str4.isalnum()  
12. print

      运行结果如下:

python 字符包含 python包含字符串函数_python 字符包含_03

组成的字符串,不包含其他字符,故经isalnum函数检测后返回值为true,而str2里有一些其他字符组成的字符串故返回值false,str3和str4里尽管有字符或者数字,但含有非字符或者数字的字符故str3和str4的isalnum测试后的值为false,这个函数在网站注册时可以用于检测注册用户名是否合法。

判断是否都是数字和是否都是字符函数

可以调用isalpha和isdigit函数来判断字符串里是否都是字符或者数字组成。


[python] view plain copy

1. str1 = 'abcd'
2. str2 = '123'
3. str3 = '1a2c'
4. t = str1.isalpha()  
5. print
6. t = str2.isalpha()  
7. print
8. t = str1.isdigit()  
9. print
10. t = str2.isdigit()  
11. print
12. t = str3.isdigit()  
13. print
14. t = str3.isdigit()  
15. print
16. t = str3.isalnum()  
17. print

       运行结果如下所示:



python 字符包含 python包含字符串函数_字符串_04


      


3判断是否都是大小写      

       islower函数可以判断字符串里的所有字符是否都是小写?isupper函数则是判断字符串里的字符是否都是大写的?


[python] view plain copy

1. str1 = 'abcd1234'
2. str2 = 'abcd'
3. str3 = 'ABCD'
4. str4 = '123abcD'
5. t = str1.islower()  
6. print
7. t = str2.islower()  
8. print
9. t = str3.islower()  
10. print
11. t = str3.isupper()  
12. print
13. t = str4.islower()  
14. print
15. t = str4.isupper()  
16. print

           运行结果如下:



python 字符包含 python包含字符串函数_bc_05



      4.1.4 判断是否有空格函数

      isspace函数可以判断字符串里是有只含有空格、\n、\t的等广义空格?



[python] view plain copy

1. str1 = 'a1 b2c'
2. str2 = ' aaa'
3. str3 = 'a\nb'
4. str4 = '\ta'
5. str5 = '\n'
6. str6 = ' '
7. str7 = '\n'
8. t = str1.isspace()  
9. print
10. t = str2.isspace()  
11. print
12. t = str3.isspace()  
13. print
14. t = str4.isspace()  
15. print
16. t = str5.isspace()  
17. print
18. t = str6.isspace()  
19. print
20. t = str7.isspace()  
21. print

        运行结果如下所示:

python 字符包含 python包含字符串函数_python_06

       判空函数isspace可以用于检查用户是否已输入。 

   4.1.5 大小写转换函数lower和upper

       这两个函数可以将大写转小写或者相反,但此两函数并未修改原值,只是原值的拷贝数据变成了全部都是小写或者全部都是大写。



[python] view plain copy

1. str1 = 'abc'
2. str2 = str1.upper()  
3. print
4. print
5. str3 = 'aBcdE'
6. str4 = str3.upper()  
7. print
8. print
9. str5 = str3.upper()  
10. print

     运行结果是:



[python] view plain copy

1. abc  
2. ABC  
3. aBcdE  
4. ABCDE  
5. ABCDE


      由上边的例子可以看出,1)这两个函数将字符串里的大写或者小写转换成小写或者大写字母。2)str3原值未发生变化,str4、str5是str3的一份拷贝后修改数据。
      4.2 strip函数

      strip是去除某字符(通常是广义空格:空格、\n、\ t 等)的函数,有两个变种lstrip和rstrip,这两个函数可以去除字符串最开始和最尾部的所有空格(不止一个)。



[python] view plain copy

1. str1 = '  a b  c  '
2. print
3. str2 = str1.lstrip()  
4. print
5. str3 = str2.rstrip()  
6. print

     代码第3行去除了str1字符串字母‘c’后边的空格,代码第5行去除了str1的最开始字母‘a’前边的空格,这两个函数在数据库写入用户输入信息时可以去除多余的空格,保留有效信息到数据库里。


     strip函数不仅可以去除空格,而且还可以去除指定字符。

    


[python] view plain copy

1. str1 = '  a b a  c c a b '
2. print
3. str2 = str1.lstrip()  
4. print
5. str3 = str2.rstrip()  
6. print
7. str4 = str3.lstrip('a')  
8. print
9. str5 = str4.rstrip('c')  
10. print
11. str6 = str4.rstrip('b')  
12. print

       代码第7行去除了字符串里的‘a’,代码第9行并没有去掉字符串里最右侧的‘c’,原因在于strip函数只能去除尾部的字符,‘c’字符没有在字符串str4尾部,而代码第11行去掉了str4尾部的‘b’这个字符,运行结果如下所示:   



[python] view plain copy

1.   a b a  c c a b   
2. a b a  c c a b   
3. a b a  c c a b  
4.  b a  c c a b  
5.  b a  c c a b  
6.  b a  c c a

      4.3 字符串的查找和替换函数


      Python提供了丰富的字符串相关库函数,其中find和replace函数可以实现子字符串的查找和替换功能。在举例使用这两个函数前,我们先看看startswith和endswith这两个函数是判断一个字符串是否以某字符串开始或者结束的。


[python] view plaincopy

1. s1 = ".com"
2. s2 = ".cn"
3. s3 = "www."
4. s4 = "www.jeapedu.com"
5.   
6. if
7. print "www"
8. if
9. print ".com"

      运行结果如下:


[python] view plaincopy

1. www  
2. .com

    好,有了对startswith和endswith函数的理解我们再看看find函数和replace的使用方法。

[python] view plaincopy


    1. # -*- coding: utf-8 -*-
    2. s1 = ".com"
    3. s3 = "www."
    4. s4 = "www.jeapedu.com"
    5.   
    6. if
    7. print "www"
    8. if
    9. print ".com"
    10. if s4.find("jeapedu"):  
    11. "jeapedu", "智普教育")  
    12. print


    replace函数通常有两个参数,第一个参数是被替换的字符串,第二个参数是替换成的字符串。

        

        4.4 join函数


    join是string类型的一个函数,用调用他的字符串去连接参数里的列表
    
       ‘,'.join调用者是',',python里面万物皆对象,','是一个string类型的对象,调用join函数,将后面的列表里的值用逗号连接
    成新的字符串;



    4.5 split函数

    split是一个非常重要的字符串函数,是join的逆方法,用来将字符串分割,返回一个字符串列表:

            

    python 字符包含 python包含字符串函数_python 字符包含_07

      函数的参数是分隔符,如果不提供任何分隔符,将以空格作为分隔符(空格,制表,换行等)

            

    python 字符包含 python包含字符串函数_python 字符包含_08