Introduction to Python's internal function of STR

Source: Internet
Author: User

Introduction to STR internal functions

Name = ' Eric ' name = Str (' Eric ')

See what types of variables belong to

Print (Type (name))

See which members are available for use

Print (dir (name))

In any language, whether in programming languages like Java,c++,c#,javascript or ruby, strings are important, so it is necessary to learn the strings well.

__contains__ ()

Whether a string contains another string
name = str (' Eric ')
RESULT1 = ' er ' in name
RESULT2 = name.__contains__ (' Er6 ')
Print (RESULT1)
>>>true
Print (RESULT2)
>>>false

Capitalize ()

Capitalize first letter
Name = ' Eric '
result = Name.capitalize ()
Print (Result)
>>>eric

Casefold ()

First Letter Lowercase
name = ' Eric '
Print (Name.casefold ())
>>>eric

Swapcase ()

Uppercase converted to lowercase, lowercase to uppercase
name = ' Eric '
result = Name.swapcase ()
Print (Result)
>>>eric

Center (PARAM1,PARAM2)

Center A string
PARAM1: Total Placeholder
PARAM2: Default is a space, you can customize
Name = ' Eric '
RESULT1 = Name.center (20)
RESULT2 = Name.center (20, ' * ')
Print (RESULT1)
>>> Eric
Print (RESULT2)
eric********

Count (Sub,start,end)

Sub: string
Start: Start position
End: Ending position
The number of occurrences of a character, or the number of occurrences of one substring
name = ' 12323222323242342 '
result = Name.count (' 2 ')
Print (Result)
>>>9

Encode ()

transcoding
name = ' Xiaoming '
result = Name.encode (' GBK ')
Print (Result)
>>>b ' \xd0\xa1\xc3\xf7 '

EndsWith (Suffix,start,end)

Suffix: Sub-string
Start: Starting position
End: Ending position
Determines whether a string ends with a character and a substring of the latter
name = ' Alex '
result = Name.endswith (' x ')
Print (Result)
>>>true

Expandtabs ()

Change tab to the default 8 spaces
name = ' A\tlex ' #\t: Indicates a space
result = Name.expandtabs ()
Print (Result)
Print (len (result))
>>>a Lex
>>>11 here because I counted \ t, so it was 11.

Find (Sub,start,end)

The position of a character or string
name = ' Alex '
result = Name.find (' Le ')
Print (Result)
>>>1
Note: Cannot find equals-1

Index ()

The position of a character or string
name = ' Alex '
result = Name.index (' Le ')
Print (Result)
Can not find the error

Format ()

Concatenation of strings in some format
(1)
Name = ' Alex {0} as {1} '
result = Name.format (' sb ', ' Eric ')
Print (Result)
>>>alex SB as Eric
(2)
Name = ' Alex {name} ' as {ID} '
result = Name.format (name= ' sb ', id= ' Eric ')
Print (Result)
>>>alex SB as Eric

Join ()

Used for stitching.
Li = [' A ', ' B ', ' C ', ' d ']
RESULT1 = "". Join (LI)
RESULT2 = "_". Join (LI)
Print (RESULT1)
>>>abcd
Print (RESULT2)
>>>a_b_c_d

Partition ()

Splitting strings as prompted
Name = "ALEXISSB"
result = Name.partition (' is ')
Print (Result)
>>> (' Alex ', ' is ', ' SB ')

Replace (Str,newstr,num)
Replace a character or substring
Name = "ALEXIDSB"
result = Name.replace ("SB", "BS")
Print (Result)
>>>alexidbs

Splitlines ()

Cutting according to the row
Name = "" "
Aa
Bb
CC "" "
result = Name.splitlines ()
Print (Result)
>>>[", ' AA ', ' BB ', ' cc ')

The internal function of the string is many, the above just introduces some common and relatively special internal functions, want to understand the string of other functions can query its source code.

Introduction to Python's internal function of STR

Related Article

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.