zoukankan      html  css  js  c++  java
  • python字符串 常用函数 格式化字符串 字符串替换 制表符 换行符 删除空白 国际货币格式

    # 字符串常用函数
    # 转大写
    print('bmw'.upper()) # BMW
    # 转小写
    print('BMW'.lower()) # bmw
    # 首字母大写
    print('how aae you ?'.capitalize()) # How aae you ?
    # 设置每个单次首字母大写
    print('michael jackson'.title()) # Michael Jackson
    # 大写转小写 小写的转大写
    print('abcDEF'.swapcase()) # ABCdef

    # 字符串格式化
    str1 = '{} {} you'.format('i','love')
    print(str1) # i love you
    # 索引 组合字符串
    str2 = '{2}.{1}.{0}'.format('com','baidu','www') # www.baidu.com
    print(str2)

    name = 'Eric'
    age = 27
    height = 180

    str3 = '我叫' + name + ',今年' + str(age) + '岁,身高' + str(height)
    print(str3)
    str4 = "我叫{},今年{}岁,身高{}".format(name,age,height)
    print(str4)
    # 推荐方法
    str5 = "我叫{name},今年{age}岁,身高{height}".format( name = name,age = age, height = height)
    print(str5)


    # 格式化数字
    num = 1234567.890134
    # 保留两位小数
    num1 = format(num, '0.2f')
    # 1234567.89
    print(num1)
    # 保留4位小数
    num1 = format(num, '0.4f')
    # 1234567.8901
    print(num1)

    # 国际货币格式
    account = '8892343'
    amt = 123456789.123
    amt1 = format(amt,',')
    # 123,456,789
    print(amt1)

    amt2 = format(amt,'0,.2f')
    # 123,456,789.12
    print(amt2)

    amt3 = "请您向" + account + "账户转账¥" + amt2 + "元"
    # 请您向8892343账户转账¥123,456,789.12元
    print(amt3)
    # 简便写法
    amt4 = "请您向{}账户转账¥{:0,.3f}元".format(account,amt)
    # 请您向8892343账户转账¥123,456,789.123元
    print(amt4)

    # 早期字符串格式化
    # 我叫张三,今年30岁,体重87.5公斤
    name = '张三'
    age = 30
    weight = 87.5
    str6 = "我叫%s, 今年%d岁, 体重%.2f公斤"%(name, age, weight)
    # 我叫张三, 今年30岁, 体重87.50公斤
    print(str6)

    # 制表符 换行符 换行
    table = '姓名 性别 年龄 赵四 男士 42'
    print(table)

    # 删除空白
    space_str = ' Hello world '
    print(space_str)
    # 计算长度
    len_str = len(space_str);
    # 13
    print(len_str)

    # strip() 删除左右空白 lstrip 删除左边空白 rstrip 删除右边空白
    no_space_str = space_str.strip();
    print(no_space_str);
    # 11
    print(len(no_space_str))

    # 字符串查找
    str7 = 'Nice to meet you, i need you help';
    index = str7.find("ee");
    # 9 返回字符串第一次出现的索引位置 从0开始计算
    print(index)

    # find(str,[start],[end])
    index2 = str7.find("ee",17);
    # 21 返回字符串出现的索引位置 从17开始计算
    print(index2)

    print('--------------------');
    # 判断是否存在
    isexist = 'ee' in str7;
    # True / False
    print(isexist);


    # 字符串替换
    # str.replace(原始串,目标串,[替换次数]) 默认全部替换
    str8 = " this is string example...wow!!! this is really string"
    pstr8 = str8.replace("is", " was")
    # th was was string example...wow!!! th was was really string
    print(pstr8)
  • 相关阅读:
    yolo_to_onnx ValueError: need more tan 1 value to unpack
    yolo_to_onnx killed
    C++ 实现二维矩阵的加减乘等运算
    Leetcode 1013. Partition Array Into Three Parts With Equal Sum
    Leetcode 1014. Best Sightseeing Pair
    Leetcode 121. Best Time to Buy and Sell Stock
    Leetcode 219. Contains Duplicate II
    Leetcode 890. Find and Replace Pattern
    Leetcode 965. Univalued Binary Tree
    Leetcode 700. Search in a Binary Search Tree
  • 原文地址:https://www.cnblogs.com/ericblog1992/p/11268669.html
Copyright © 2011-2022 走看看