zoukankan      html  css  js  c++  java
  • Python知识点梳理(1):字符串

    1.字符串是数组

    像许多其他流行的编程语言一样,Python中的字符串是表示unicode字符的字节数组。

    1 a = "Hello, World!"
    2 print(a[1])

    1.1裁切

    可以使用裁切语法返回一定范围的字符。

    1 b = "Hello, World!"
    2 print(b[2:5])  #获取从位置2到位置5(不包括)的字符

    1.2负的索引

    1 #获取从位置5到位置1的字符,从字符串末尾开始计数
    2 b = "Hello, World!"
    3 print(b[-5:-2])

    2.字符串方法

     Python有一组可用于字符串的内置方法。

    strip()方法删除开头和结尾的空白字符

    1 a = " Hello, World! "
    2 print(a.strip()) # returns "Hello, World!"

    lower() 返回小写的字符串

    1 a = "Hello, World!"
    2 print(a.lower())

    upper() 方法返回大写的字符串

    a = "Hello, World!"
    print(a.upper())

    replace() 用另一段字符串来替换字符串

    1 a = "Hello, World!"
    2 print(a.replace("World", "Kitty"))
  • 相关阅读:
    6、查看历史记录
    A Tour of Go Range
    Go Slices: usage and internals
    A Tour of Go Nil slices
    A Tour of Go Making slices
    A Tour of Go Slicing slices
    A Tour of Go Slices
    A Tour of Go Arrays
    A Tour of Go The new function
    A Tour of Go Struct Literals
  • 原文地址:https://www.cnblogs.com/memory-ccy/p/13073392.html
Copyright © 2011-2022 走看看