zoukankan      html  css  js  c++  java
  • Python入门系列教程(二)字符串

    字符串

     1.字符串输出

    name = 'xiaoming'
    print("姓名:%s"%name)

    2.字符串输入

    userName = raw_input('请输入用户名:')
    print("用户名为:%s"%userName)
    password = raw_input('请输入密码:')
    print("密码为:%s"%password)

    3.常用的格式符号

    格式符号转换
    %c 字符
    %s 通过str() 字符串转换来格式化
    %i 有符号十进制整数
    %d 有符号十进制整数
    %u 无符号十进制整数
    %o 八进制整数
    %x 十六进制整数(小写字母)
    %X 十六进制整数(大写字母)
    %e 索引符号(小写'e')
    %E 索引符号(大写“E”)
    %f 浮点实数
    %g %f和%e 的简写
    %G %f和%E的简写

    4.字符串的索引(下标)

    name = 'abcdef'
    print(name[0])
    print(name[1])
    print(name[2])

    5.切片

    name = 'abcdef'
    print(name[0:6:2])

    输出:ace

    print(name[-1])

    输出:f

    print(name[3:])

    输出:def

    print(name[-1:3:-1])

    输出:ef

    print(name[::-1])

    输出:fedcba

    6.字符串常见操作

    (1)find

    mystr = 'hello world'
    print mystr.find('w', 0, len(mystr))

    输出:6

    -1表示没有搜索到结果

    (2)index

    print mystr.index('w', 0, len(mystr))

    输出:6

    找不到直接报异常

    (3)count:返回 str在start和end之间 在 mystr里面出现的次数

    print mystr.count('h', 0, len(mystr))

    输出:0

    (4)replace:把 mystr 中的 str1 替换成 str2,如果 count 指定,则替换不超过 count 次

    print mystr.replace('h', 'H',  1)

    输出:Hello world

    (5)split:以 str 为分隔符切片 mystr,如果 maxsplit有指定值,则仅分隔 maxsplit 个子字符串

    print  mystr.split(" ", 2)

    输出:['hello', 'world']

    (6)capitalize:把字符串的第一个字符大写

    print mystr.capitalize()

    输出:Hello world

    (7)lower和upper

    print mystr.lower()
    print mystr.upper()

    (8)startswith和endswith

    print  mystr.startswith('h')
    print  mystr.endswith('d')

    (9)ljust:返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串

        rjust:返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串

    print  mystr.ljust(10)
    print  mystr.rjust(10)

    (10)lstrip:删除 mystr 左边的空格

         rstrip:删除 mystr 字符串末尾的空格

    print  mystr.lstrip()
    print  mystr.rstrip()

    (11)不过是从右边开始查找

    print mystr.rfind('w')
    print mystr.rindex('w')

    (12)mystr.partition(str):把mystr以str分割成三部分,str前,str和str后

    mystr = 'you and  me'
    print mystr.partition('and')

    输出:('you ', 'and', '  me')

        mystr.rpartition(str):类似于 partition()函数,不过是从右边开始.

    (13)mystr.splitlines():按照行分隔,返回一个包含各行作为元素的列表

    (14)isalnum

      如果 mystr 所有字符都是字母或数字则返回 True,否则返回 False

      mystr.isalnum()

    (15)isalpha

      如果 mystr 所有字符都是字母 则返回 True,否则返回 False

      mystr.isalpha()

    (16)isdigit

      如果 mystr 只包含数字则返回 True 否则返回 False.

      mystr.isdigit()

    (17)isspace

      如果 mystr 中只包含空格,则返回 True,否则返回 False.

      mystr.isspace()

    (18)isupper

      如果 mystr 所有字符都是大写,则返回 True,否则返回 False

      mystr.isupper()

    (19)join

      mystr 中每个字符后面插入str,构造出一个新的字符串

      mystr.join(str)

  • 相关阅读:
    Redis和Memcache区别,优缺点对比
    C# 修改Config文件,增删查改
    SQL SERVER-时间戳(timestamp)与时间格式(datetime)互相转换
    post数据时报错:远程服务器返回错误: (400) 错误的请求。
    Win10 通过附加进程调试时出现“此任务要求应用程序具有提升的权限”
    谈谈技术原则,技术学习方法,代码阅读及其它
    C# 对象与JSON字符串互相转换的三种方式
    JSON.stringify 语法实例讲解
    前端Json对象与Json字符串互转(4种转换方式)
    Razor引擎学习:RenderBody,RenderPage和RenderSection
  • 原文地址:https://www.cnblogs.com/cnki/p/6964680.html
Copyright © 2011-2022 走看看