String type of Python data structure (str)

Source: Internet
Author: User

String Type (str)
    • String description
    • Escape character
    • String operators
    • String formatting
    • Functions built into strings
I. String description

The string is the most commonly used data type in Python. We can use quotation marks (' or ') to create a string.

To create a string, simply assign a value to the variable, such as: Str = "Hello World"

  To access the values in the string:

Python does not support single character types, and single-character is used as a string in Python.

Print ("helloWorld") Hello World

  String Update:

You can intercept a part of a string and stitch it with other fields, as in the following example:

Print ("Hello World" , " Hi, China ") Hello World Hello, China
Two. Escape character

Python uses a backslash (\) to escape characters when special characters are needed in characters. As the following table:

Three. String operators

The following table instance variable a value is the string "Hello" and the B variable value is "Python":

Four. String formatting

Python supports the output of formatted strings. Although this may use a very complex expression, the most basic usage is to insert a value into a string that has the string format of%s.

In Python, string formatting uses the same syntax as the sprintf function in C.

Print (" the capital of China is%s" % (" Beijing ")) The capital of China is Beijing

Python string formatting symbols:

Formatting operator Auxiliary directives:

Five. Functions built into strings

  1.capitalize (self)

Description: Converts the first character of a string to uppercase, and other letters to lowercase; note that the original string contents are not changed;

Syntax:str. Capitalize()

Return value: The method returns a first-letter uppercase string;

Instance:

" Hello World " Print (S.capitalize ()) Hello World

  2.casefold (self)

Description: Converts all uppercase characters in a string to lowercase characters; note that the original string contents are not changed;

Syntax: Str.casefold ()

Return value: Returns the string generated when all uppercase characters in the string are converted to lowercase;

Instance:

" Hello World " Print (S.casefold ()) Hello World

  3.center (self, width, fillchar=none)

Description: Returns a specified width and center string, Fillchar as a filled character, and the default is a space;

Syntax: Str.center (Width,fillchar)

Return value: Returns a string that specifies the width of the center, if width is less than the string width, and returns the string directly, otherwise Fillchar is used to populate.

Instance:

Print (" today's headline ". Center (+,"*"))****************** today's headline * * ****************

  4.count (self, sub, Start=none, End=none)

Description: Used to count the number of occurrences of a character in a string. The optional parameters are the start and end positions of the string search.

Syntax: Str.count (Sub,start=none, End=none)

Parameters: Sub-----> substring to search;

Start-----> where the string starts searching. The default is the first character, and the first character index value is 0;

End------> The location of the ending search in the string. The index of the first character in a character is 0. The default is the last position of the string;

Return value: Returns the number of occurrences of a substring in a string;

Instance:

Print (Ss.count ("a")) Print (Ss.count ("a", 0,8))1

  5.encode (self, encoding= ' utf-8 ', errors= ' strict ')

Description: encodes A string in the specified encoding format. The errors parameter can specify different error handling scenarios.

Syntax: Str.encode (encoding= ' utf-8 ', errors= ' strict ')

Parameters: Encoding-----> Encoding to use, such as: UTF-8

Errors-----> Set up a different error handling scheme. The default is ' strict ', which means a unicodeerror is caused by a coding error. Other possible values are ' ignore ', ' replace ', ' xmlcharrefreplace ', ' backslashreplace ' and any value registered through Codecs.register_error ().

Return value: Returns the encoded string, which is a bytes object

Instance:

>>> ABC ="The lights are Dim">>> Abc_utf8 = Abc.encode ("UTF-8")>>> ABC_GBK = Abc.encode ("GBK")>>>Print(ABC) Lights Dim>>>Print(Abc_utf8) b'\xe7\x81\xaf\xe7\x81\xab\xe9\x98\x91\xe7\x8f\x8a'>>>Print(ABC_GBK) b'\xb5\xc6\xbb\xf0\xc0\xbb\xc9\xba'

  6.decode (self, decoding= ' utf-8 ', errors= ' strict ')

Description: decodes A string in the specified encoding format. The errors parameter can specify different error handling scenarios.

Syntax:bytes. Decode(encoding="Utf-8", errors="Strict")

Parameters: Encoding-----> Encoding to use, such as: UTF-8

Errors-----> Set up a different error handling scheme. The default is ' strict ', which means a unicodeerror is caused by a coding error. Other possible values are ' ignore ', ' replace ', ' xmlcharrefreplace ', ' backslashreplace ' and through codec Any value registered by the S.register_error ().

Return value: Returns the decoded string;

Instance:

Print (Abc_gbk.decode ("gbk")) Lights Dim Print (Abc_utf8.decode ("UTF-8")) Lights Dim

  7.endswith (self, suffix, start=none, end=none)

Description: Used to determine whether the string ends with the specified suffix, or false if the end of the specified suffix returns true;

Syntax: Str.endswith (Suffix,start=none, End=none)

Parameters: Suffix-----> This parameter can be a string or an element;

Start------> Starting position in the string;

End position------> character

Return value: Returns True if the string contains the specified suffix, otherwise false;

Instance:

" Hello World " Print (End.endswith ("ld")) True Print (End.endswith ("ld", 0,10)) False

  8.expandtabs (self, tabsize=8)

Note: the tab symbol (' \ t ') in the string is converted to a space, and the tab symbol (' \ t ') has a default number of spaces of 8;

Syntax: Str.expandtabs (tabsize=8)

Parameters: Tabszie-----> Specify the number of characters in the conversion string for the tab symbol (' \ t ') to a space;

Return value: Returns the new string generated when the tab symbol (' \ t ') in the string is converted to a space;

Instance:

" This is \tstring example " Print  is a     string exampleprint is string Example Print (Ss.expandtabs (is             string example

  9.find (self, sub, Start=none, End=none)

Description: Detects whether a substring sub is contained in a string, if the start (start) and end (end) ranges are specified, the check is included in the specified range, and if the specified index value is included in the specified range, the starting position of the index value in the string is returned. Returns 1 if the index value is not included.

Syntax: Str.find (Sub, Start=none, End=none)

Parameters: Sub------> Specifies the retrieved string;

Start-----> Specify start index location, default is 0

End-----> Specify the end index position, which defaults to the string length, Len (str)

Return value: Returns 1 if the containing substring returns the starting index value;

Instance:

" Life was too short for python " Print (Str1.find ("short")) Print (Str1.find ("short", 5,19))12
>>> Print (Str1.find ("short", 17,19))
-1

format (self, *args, **kwargs)

Description

String type of Python data structure (str)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.