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'))
  • 相关阅读:
    JavaScript异步编程1——Promise的初步使用
    Pailler
    ElGamal
    RSA
    密码基础
    博客园中:为文章添加版权保护
    DCT实现水印嵌入与提取(带攻击)
    量子:基于EPR块对的两步量子直接通信
    量子:拜占庭协议和测谎问题的量子协议的实验证明
    liunx:网络命令
  • 原文地址:https://www.cnblogs.com/xiaoyingying/p/7693260.html
Copyright © 2011-2022 走看看