The method of the string source code is superficial interpretation

Source: Internet
Author: User

Method of STR string:

    1. 1. There are two ways of creating a string first

S= "123" or S=name

S1=STR (123)

2. How to capitalize the initial letter capitalize ()

>>> s= "Alex"

>>> s1=s.capitalize ()

>>> Print (S1)

Alex

3. string Casefold () method , restore the original value when the string was created

Can be seen by knocking out the following code

#首先创建一个字符串s, make its first character uppercase, and then execute the Casefold () method to make it look the same

>>> s= "Alex"

>>> s1=s.capitalize ()

>>> Print (S1)

Alex

>>> S2=s1.casefold ()

>>> Print (s2)

Alex

#让s的全部字母大写赋值给s2, then S2 uses the Casefold () method to restore the original value and assign it to S3

>>> S2=s.upper ()

>>> Print (s2)

ALEX

>>> S3=s2.casefold ()

>>> Print (S3)

Alex

4 . The center () method of the string :

S.center (Width,[,fillchar])->str, you can use the following demo to understand

#首先创建一个字符串alex, and then using the center () method, the first parameter width is the length of the total value specified after the method is executed, the following length is 20, and then you can see that the length of the original S is len (s) = 4, then the length of the 16 bits is the second parameter " * "To fill, Alex occupies the middle of the 4 bits

>>> s= "Alex"

>>> Len (s)

4

>>> Print (S.center (20, "*"))

alex********

5. The count () method of the string

>>> s= "Alexalexalex"

#计算字母a在字符串s中出现的次数, the second parameter 0 refers to the search starting at the first index position of S, and the third parameter 10 refers to the position where the tenth index of the string s is pointing.

>>> S.count ("a", 0,10)

3# The result is a in s that appears 3 times

>>> S.count ("L", 4) #可以看到第三个参数如果没指定就是默认到s的最后一个字母为结束位置

The result is 2 times.

>>> S.count ("a") #可以看到第二个和第三个参数如果没指定, is the default entire s within the search

3# results are 3 times

5. encode the string encode () and decode the decode () method:

#-*-coding:utf-8-*-

A= "Lu Li" #创建一个字符串, the specified format is utf-8

A_unicode=a.decode ("Utf-8") #先把a解码成unicode格式

A_gbk=a_unicode.encode ("GBK") #再把a_unicode编码成gbk形式

6. String formatting method and format () method

#1. Using the format method to pass parameters sequentially to achieve similar formatting effects

>>> s= "Name:{0},age:{1}"

>>> Print (S.format ("Alex", "18"))

Name:alex,age:18

#2. Formatted directly by%,%s can be substituted for all types including string, integer, etc., and%d,%f refers to integer and floating-point values respectively.

>>> s= "name:%s,age:%s"% ("Alex", 18)

>>> print (s)

Name:alex,age:18

7.index () Find Index Method

S.index (sub[, start[, end]), int #在字符串S中查找字母sub, the start and end parameters specify the starting and ending positions, respectively, the second and third parameters can be unspecified, and the default is to look from beginning to end, as follows:

>>> s= "Alex"

>>> S.index ("a")

0

>>> S.index ("L", 0, 3)

1

8. the method with is in the string , such as:

S.isalnum (), bool #判断字符串是否只包含字母和数字, if it is, returns a Boolean value of true, otherwise it is false

S.isalpha (), bool #判断字符串是否只包含字母, if it is, returns a Boolean value of true, otherwise it is false

S.isdigit (), bool #判断字符串是否Unicode数字, byte digit (single byte), Full width digit (double byte), Roman numeral, if yes returns Boolean true, otherwise false

S.isdecimal (), bool #判断字符串是否只包含十进制的字符串, if it is, returns a Boolean value of true, otherwise it is false

S.isspace (), bool #判断字符串是否只包含空格, if it is, returns a Boolean value of true, otherwise it is false

S.istitle (), bool #判断一个字符串的每个词的首个字母是否大写, returns True if it is, otherwise returns false,

S.title () The method is to change the string to a title, as follows

>>> s= "The One"

>>> S.istitle ()

False

>>> S1=s.title ()

>>> S1.istitle ()

True

9. the Join () method of the string : S.join (iterable), str # The parameters must be iterative, and the following actions

>>> cha= "." #创建一个分隔符句号 "."

>>> s1= ("Alex", "name") #创建一个元组, tuples are iterative

>>> s2=["Alex", "name"] #创建一个列表, the list is iterative

>>> s3={"K1": "Alex", "K2": "Name"} #创建一个字典, the dictionary is an iterative

>>> Cha_s1=cha.join (S1)

>>> Print (CHA_S1)

Alex.name #可看到最后结果是用句号把元组里的两个元素结合成一个字符串

>>> cha_s2=cha.join (S2)

>>> Print (CHA_S2)

Alex.name

>>> Cha_s3=cha.join (S3)

>>> Print (CHA_S3)

K2.k1

>>> s4=s3.values ()

>>> Print (S4)

Dict_values ([' Name ', ' Alex '])

>>> Cha_s4=cha.join (S4)

>>> Print (CHA_S4)

Name.alex

Ten . Ljust () and Rjust () left and right alignment methods:

>>> s= "Alex"

>>> S.ljust (10, "*")

' alex****** '

>>> S.rjust (10, "*")

' ******alex '

S.partition (Sep) (head, Sep, tail)

>>> s= "Alleall"

>>> s.partition ("E")

(' All ', ' e ', ' all ')

S.replace (old, new[, Count]) str

>>> S.replace ("A", "B") #把a替换成b, no specified number of times, is the default all a is replaced by a B

' BLLEBLL '

>>> S.replace ("A", "B", 2)

' BLLEBLL '

>>> S.replace ("A", "B", 3)

' BLLEBLL '

S.split (Sep=none, maxsplit=-1), List of strings

>>> s= ("Name:age:class:school")

>>> s.split (":")

[' Name ', ' Age ', ' class ', ' School '] #以冒号作分隔符 and return the result to a list

startswith (...) and EndsWith () # determines whether a string starts and ends with the given argument letter, and returns True , otherwise returns false

>>> s= "Alex"

>>> S.startswith ("a")

True

>>> S.endswith ("x")

True

the. Find () method # finds the letters a and e from the string s andreturns their positions

>>> s= "Alex"

>>> S.find ("a")

0

>>> s.find ("E")

2

>>> S.find ("A", 0, 3)

0

16.lstrip () , Rstrip () , strip () Method

is to remove the left, right, and left spaces of the string, respectively.

>>> s= "Alex"

>>> S.lstrip ()

' Alex '

>>> S.rstrip ()

' Alex '

>>> S.strip ()

' Alex '

The method of the string source code is superficial interpretation

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.