zoukankan      html  css  js  c++  java
  • python基本语法2.5--字符串的相关操作

    # -*- coding: utf-8 -*-
    
    import string
    
    # strip去除空格
    s = ' abcd efg  '
    print(s.strip())
    print(s.lstrip())
    print(s.rstrip())
    print(s)
    
    # 字符串连接
    print('abc_' + 'defg')
    s = 'abcdefg'
    s += '
    hijk'
    print(str)
    
    # 大小写
    s = 'abc defg'
    print(s.upper())
    print(s.upper().lower())
    print(s.capitalize())
    
    # 位置和比较
    s_1 = 'abcdefg'
    s_2 = 'abdefgh'
    print(s_1.index('bcd'))
    try:
        print(s_1.index('bce'))
    except ValueError:
        print('ValueError: substring not found')
    print(s_1 == s_1)   # cmp函数被Python3移除了
    print(s_1 > s_2)
    print(s_2 > s_1)
    
    # 分割和连接
    s = 'abc,def,ghi'
    print(s.split(','))
    s = '123
    456
    789'
    numbers = s.splitlines()
    print(numbers)
    print('-'.join(numbers))
    
    # 常用判断
    s = 'abcdefg'
    print(s.startswith('abc'))
    print(s.endswith('efg'))
    print('abcd1234'.isalnum())
    print('	abcd1234'.isalnum())
    print('abcd'.isalpha())
    print('12345'.isdigit())
    print('  '.isspace())
    print('acb125'.islower())
    print('A1B2C'.isupper())
    print('Hello world!'.istitle())
    
    # 数字到字符串
    print(str(5))
    print(str(5.))
    print(str(-5.23))
    print(int('1234'))
    print(float('-23.456'))
    
    # 格式化字符串
    print('Hello %s!' % 'world')
    print('%d-%.2f-%s' % (4, -2.3, 'hello'))
  • 相关阅读:
    【2020NOI.AC省选模拟#5】C. 光滑序列
    【2020NOI.AC省选模拟#2】A. 旋转
    【2020NOI.AC省选模拟#1】B. Trie
    Redis 配置
    Redis 删除策略
    Redis 事务
    Redis 持久化
    Redis 通用指令
    Redis 数据类型实战案例
    Redis sorted_set
  • 原文地址:https://www.cnblogs.com/xiaoyingying/p/7693260.html
Copyright © 2011-2022 走看看