zoukankan      html  css  js  c++  java
  • 003: 基本类型-字符串类型

    1)They can be enclosed in single quotes ('...') or double quotes ("...") with the same result.  can be used to escape quotes

    2)If you don’t want characters prefaced by  to be interpreted as special characters, you can use raw strings by adding an r before the first quote

    3)String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''. End of lines are automatically included in the string, but it’s possible to prevent this by adding a  at the end of the line. The following example

    4)Strings can be concatenated (glued together) with the + operator, and repeated with *

    
    
    # equals to 'xxxxx'
    'x' * 5

    5)Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated.This only works with two literals though, not with variables or expressions.

    6)Strings can be indexed (subscripted), with the first character having index 0. There is no separate character type; a character is simply a string of size one.

    7)Indices may also be negative numbers, to start counting from the right.Note that since -0 is the same as 0, negative indices start from -1.

    8)In addition to indexing, slicing is also supported. While indexing is used to obtain individual characters, slicing allows you to obtain substring. Note how the start is always included, and the end always excluded. This makes sure that s[:i] + s[i:] is always equal to s. Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.

    9)Attempting to use an index that is too large will result in an error. However, out of range slice indexes are handled gracefully when used for slicing.

    10)Python strings cannot be changed — they are immutable. Therefore, assigning to an indexed position in the string results in an error.

    11)The built-in function len() returns the length of a string

    12)比较常用的方法是:find, 它可以返回子串首次匹配的位置:

    'hello'.find('l') # return 2

     

    上面所述都是string的基本特点,另外string本身也是一个class,所以它还有很多其它的方法,而且很多,API地址如下:

    https://docs.python.org/3/library/stdtypes.html#string-methods

  • 相关阅读:
    利用反射获取类的所有字段
    System.Data.Entity.Internal.AppConfig 类型初始值设定项引发异常
    sql server 修改列类型
    sql server 删除表字段和字段的约束
    sql server 查找字段上的约束
    ASP.NET MVC自定义路由
    初探pandas——索引和查询数据
    初探pandas——安装和了解pandas数据结构
    初探numpy——numpy常用通用函数
    初探numpy——广播和数组操作函数
  • 原文地址:https://www.cnblogs.com/jcsz/p/5087135.html
Copyright © 2011-2022 走看看