Python__字符串总结

1. 字符串的定义:(ctrl+/)注释/取消注释

                单引号、双引号、三引号(其中三引号也可以用来做注释)

       str  = "aad222"

       str = 'ss22'

     str = ''' ss222'''

2字符串拼接(+号)

      print('hello'+'world')     ----   helloworld

      print('hello'+'3')            ----   hello3

      print('hello'*3)              ----- hellohellohello

      print('hello '*3)             ----  hello hello hello 

      print(3,'hello')               ---  3 hello

 

3.序列操作

        序列定义:一个序列,若干个元素组成;一般下标从0开始

       常用操作:

        a.获取元素

    str[10]/str[len(str)-1]/str[-1]

        b.求元素个数

    len(str)

  c. 获取某元素的下标

         str.index('b')

       d.切片操作

          string[start:end]:从start开始,到end结束,但不包含end

          string[start:]:从start开始,到字符串结束

     string[:end]:从第一个字符开始,到end位置结束,但不包括end

     string[start:end:步长]:步长默认是1

     字符串倒序:string[::-1]

     正下表:从左向右:0,1,2,3,4

          负下标:从右向左:-1,-2,-3,-4

    in : 成员运算符 - 如果字符串中包含给定的字符返回 True

    可以使用find方法,不等于-1则表示找到  

      例如:   str = 'hello'

          'h’ in str  ----- True

        not in : 成员运算符 - 如果字符串中不包含给定的字符返回 True

      例如: str = 'hello'

         'a'  not  in  str   --  True

       r/R : 原始字符串 - 原始字符串:所有的字符串都是直接按照字面的意思来使用,没有转义特殊或不能打印的字符。 原始字符串除在字符串的第一个引号前                加上字母"r"(可以大小写)以外,与普通字符串有着几乎完全相同的语法。

                    例如: open(r"d:\project\aa.txt")

        % :  格式化字符串

 

5.字符串常用的方法

       1> str.count():计算字符串中包含多少个指定的子字符串

    info = "abcdefa"

    print (info.count('a')) ----  2

  2> str.startswith():检查字符串是否以指定的字符串开头

    info = "this is tom"

    print (info.startswith("this")) --- True

  3> str.endswith() :检查字符串是否以指定的字符串结尾

    info = "this is tom"

    print (info.endswith("tom")) --  True

  4> str.find()  :返回指定的子字符串在字符串中出现的位置

    "123456789".find("456") ----  3

    "ok,good,name".find(',')  ---- 2 (有多个则返回第一个)

    “ok,name”.find('o',3) -- 4(指定位置开始找,正序找)

    “ok,-1) -- 0(指定位置开始查找:倒序只找最后一个元素)

  5> str.isalpha() :检查字符串中是否都是字母

    "abc1".isaplha()   --  False

  6> str.isdigit() :检查字符串中是否都是数字

    "123123",isdigit() --- True

  7> str.join():将sequence序列类型的参数的元素字符串合并(连接到一个字符串)

    print("##".join(['i','like','play','football!'])) -- i##like##play##football

    print("##".join(('i','football!'))) -- i##like##play##football

  8> str.split():将字符串分割为几个子字符串,参数为分隔符,返回list列表

      被分割的切点会被拿掉

    info = 'name is tom '

    print(info.split(' '))   ---- ['name','is','tom','']

   9>  str.lower() :将字符串里面如果有大写字母的全部转为小写字母

  10> str.opper():将字符串里面如果有小写字母的全部转为大写字母

  11> str.replace():替换字符串里面指定的子字符串

      info = "name is tom"

      print(info.replace('tom','jack'))  ---  name is jack

  12> str.strip() :将字符串前置空格和后置空格删除

      "   good   ".strip()  --- 'good'

  13> str.lstrip():去除前置空格

      "   good   ".strip()  --- 'good    '

  14> str.rstrip():去除后置空格

      "   good   ".strip()  --- '     good'

相关文章

Python中的函数(二) 在上一篇文章中提到了Python中函数的定...
Python中的字符串 可能大多数人在学习C语言的时候,最先接触...
Python 面向对象编程(一) 虽然Python是解释性语言,但是它...
Python面向对象编程(二) 在前面一篇文章中谈到了类的基本定...
Python中的函数(一) 接触过C语言的朋友对函数这个词肯定非...
在windows下如何快速搭建web.py开发框架 用Python进行web开发...