zoukankan      html  css  js  c++  java
  • 常用的字符串方法

    常用的字符串方法

    7个基本:join,split,find,strip,upper,lower,replace

    5个深度:1.for循环index;2.索引,下标,获取字符串中的某一个字符;3.切片;4.获取长度;5.获取连续或不连续的数字;

    str="1r大自然的景色真美,让我流连忘返啊R"
    test_str='+'.join(str)
    print(test_str)
    test_str = str.split('')
    print(test_str)
    test_str = str.find('')
    print(test_str)
    test_str = str.strip("1")
    print(test_str)
    test_str = str.upper()
    print(test_str)
    test_str = str.lower()
    print(test_str)

    运行结果

    1+r+大+自+然+的+景+色+真+美+,+让+我+流+连+忘+返+啊+R
    ['1r', '自然的景色真美,让我流连忘返啊R']
    2
    r大自然的景色真美,让我流连忘返啊R
    1R大自然的景色真美,让我流连忘返啊R
    1r大自然的景色真美,让我流连忘返啊r

    test = "妹子有种冲我来"
    index = 0
    while index < len(test):
        v = test[index]
        print(v)
    
        index += 1
    print('=======')
    
    for zjw in test:
        print(zjw)
    
    
    
    # 二、索引,下标,获取字符串中的某一个字符
    v = test[3]
    print(v)
    
    # 三、切片
    v = test[0:-1] # 0=<  <1
    print(v)
    
    # 四、获取长度
    # Python3: len获取当前字符串中由几个字符组成
    v = len(test)
    print(v)
    
    # 注意:
    # len("asdf")
    # for循环
    # 索引
    # 切片
    
    # 五、获取连续或不连续的数字,
    # Python2中直接创建在内容中
    # python3中只有for循环时,才一个一个创建
    r1 = range(10)
    r2 = range(1,10)
    r3 = range(1,10,2)
    # 帮助创建连续的数字,通过设置步长来指定不连续
    v = range(0, 100, 5)
    #
    for item in v:
        print(item)
  • 相关阅读:
    973. K Closest Points to Origin
    919. Complete Binary Tree Inserter
    993. Cousins in Binary Tree
    20. Valid Parentheses
    141. Linked List Cycle
    912. Sort an Array
    各种排序方法总结
    509. Fibonacci Number
    374. Guess Number Higher or Lower
    238. Product of Array Except Self java solutions
  • 原文地址:https://www.cnblogs.com/hanjiangs/p/14197913.html
Copyright © 2011-2022 走看看