• python字符串方法学习笔记


    # 一、字符串大小写转换
    # 字符串首字符大写
    print("hello world".capitalize())
    # 将字符串变为标题
    print("hello WORLD".title())
    # 将字符串转为大写
    print("hello world".upper())
    # 把字符串转为小写
    print("HELLO WORLD".lower())
    # 翻转字符串中的大小写
    print("hello WORLD".swapcase())

    # 二、字符串分割
    # 以某个元素为分割点,将字符串分为3部分,从左往右找到的第一个元素为分割点
    print('helloworld'.partition('o'))
    # 以某个元素为分割点,将字符串分为3部分,从右往左找到的第一个元素为分割点
    print('helloworld'.rpartition('o'))
    # 替换原字符串中的元素,默认全部替换,可以指定替换几个(从左往右数)
    print("hello world".replace('o', 'a', 1))
    # 以某个元素为分割点,将字符串分割,从左往右分割n次
    print("hello world".split('o', 1))
    # 以某个元素为分割点,将字符串分割,从右往左分割n次
    print("hello world".rsplit('o', 1))
    # 按照行(' ', ' ', ')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。
    print('hello world'.splitlines(True))
    # 三、字符串中查找元素
    # 统计某个字符串从索引n到y出现的次数,缺省为在整个字符串中查找
    print("hello world".count('o', 7, 10))
    # 在索引[n , y)之间查找元素,缺省为[:]返回元素的索引,如果没找到返回-1
    print("hello world".find('e'))
    print("hello world".find('o', 0, 2))
    # 在[n, y)之间找元素的索引值,没找到会报错
    print("hello world".index('o'))
    print("hello world".index('e', 0, 5))
    # 四、字符串判断
    # 判断字符串是否以某个元素开始
    print('helloworld'.startswith('h'))
    # 判断字符串的的从[n,y)的索引之间是否以某个字符结尾,返回值为布尔值
    print("hello world".endswith('e', 0, 2))
    # 判断是否是只有数字或字母
    print('abc123'.isalnum())
    # 判断是否只含有字母
    print('abc'.isalpha())
    # 判断字母是否都是小写
    print("Hello".islower())
    # 判断字符是不是空格
    print(" ".isspace())
    # 判断是不是字符串是不是标题(单词首字母是不是大写)
    print("Hello World".istitle())
    # 在元素之间插入指定字符
    # 五、字符串格式化
    # 字符串居中,规定字符串的总长度,不够用其他字符补齐,默认是空格
    print("hello world".center(20, "#"))
    # 把字符串中的 替换为n个空格
    print("hello world".expandtabs(tabsize=20))
    print('#'.join("hello world"))
    # 规定输出字符的长度,并且左对齐,不足部分用指定字符补齐
    print("hello world".ljust(20, "#"))
    # 规定输出字符的长度,并且右对齐,不足部分用指定字符补齐
    print("hello world".rjust(20, "#"))
    # 去除字符串左边的的空格
    print(' hello'.lstrip())
    # 去除字符串右边的的空格
    print('hello '.rstrip())
    # 去除字符串两边的的空格
    print(' hello '.strip())
    # 指定字符串的长度,不够在前面补0
    print("123".zfill(5))
    # 字符串的拼接
    print('hello ' + 'world')
    print('hello ' * 3)

  • 相关阅读:
    some tips
    ORA00847: MEMORY_TARGET/MEMORY_MAX_TARGET and LOCK_SGA cannot be set together
    Chapter 01Overview of Oracle 9i Database Perfomrmance Tuning
    Chapter 02Diagnostic and Tuning Tools
    变量与常用符号
    Chapter 18Tuning the Operating System
    标准输入输出
    Trace files
    DBADeveloped Tools
    Chapter 03Database Configuration and IO Issues
  • 原文地址:https://www.cnblogs.com/gaoyuanzhi/p/8232682.html
Copyright © 2020-2023  润新知