Common operations for data type &STR

Source: Internet
Author: User

1. Digital Int.

The numbers are mainly used for computational purposes.

2. String str

Index and slice of a string

Index is subscript, that is, the elements of a string start with the first, the initial index is 0 and so on

s= (' ABCDEFG ') print (s[0])    #  Aprint (s[1])    #  B

A slice is a segment of a string that is truncated by an index (index start: Index end: Step), forming a new string (in principle Gu Tou regardless of the tail).

s= (' ABCDEFG ') print (S[0:3])    #  Abcprint (s[1:])      #  Bcdefgprint (S[1:6:2])   #bdfprint (S[-1::-2])    #步长-2 Geca

3. Boolean bool

There are two types of Boolean values: True,false. is the correct condition of the reaction.

4. List of lists

One of the underlying data types in Python is enclosed in [], with each element separated by commas, and he can hold a variety of data types.

5. Yuan zu Tupe

Ganso is also known as a read-only list, where data can be queried but cannot be changed.

6. Dictionary Dict

Dictionaries are used to store large amounts of relational data, enclosed in {}.

Conversion of data types:

1. Conversion string of character and number to digital   i.e.   int (str) condition:        Note:   str must be a numeric string number to convert to string   That is, str (int ) Condition 2. Converts a Boolean value to a number to a Boolean value:   when the number is 0               Boolean value of false                                                When numbers are non-0          Boolean value True      Boolean converted to digital:   when the Boolean value is true    The number can be converted to 1                                               when the Boolean value is false , the number can be converted to 03. Boolean and String conversions: for example: s= ('   ')   string content is empty, Boolean value false            s= (' abc ')   string content is not empty, the Boolean value true  the operation of the string: Example: s= ("ADFJFFJFA" )

(1) First letter capitalization

s= (' ADFJFFJFA ') s1=s.capitalize () print (S1)

Adfjffjfa

(2) Case reversal

s= (' ADFJFFJFA ') s1=s.swapcase () print (S1)

Adfjffjfa

(3) All caps

s= (' ADFJFFJFA ') S1=s.upper () print (S1)     #结果为ADFJFFJFA

(4) Change all lowercase

s= (' ADFJFFJFA ') S1=s.lower () print (S1)     #结果为adfjffjfa

(5) Capitalize the first letter of each separated element

Cases

s= (' ADFJ ff JFA ') S1=s.title () print (S1)     #结果为ADFJ FF JFA

(6) Number of occurrences of counting elements

s= (' ADFJ ff JFA ') S1=s.count (' F ') print (S1)     #结果为3

(7) Delete

Strip (): The default is to remove the leading and trailing spaces that are used to remove strings, or to delete characters

Lstrip (): Delete the character to the left of the string

Rstrip (): Delete character to the right of the string

(8) Index Search by element

Find: Returns 1 if the corresponding index is not found

The default here is to return to the location of the first one found.

s= (' Keep your self ') s1=s.find (' E ') print (S1)     #结果为1

Index: Error if no corresponding index is found

s= (' Keep your self ') s1=s.index (' E ') print (S1)     #结果为1

(9) Judgment on what to start/end with: Startwish, endwish

s= (' Keep your self ') s1=s.startwith (' ka ') print (S1)     #结果为False

Endwish

(10) Solving the number of characters in a string

s= (' Keep your self ') print (Len (s))                   #结果为14, spaces are also counted characters        

(11) Split: Split to break a string into a list

s= (' Keep your self ') s1=s.split (' E ') print (S1)     #结果为 [' k ', ' ', ' p ' your s ', ' LF ']

(12) Determine if the character in the string is a number and the return value is a bool value.

S= (' a ') S1=s.isdigit () print (S1)    #结果是False

(13) Fill Center (length, character to fill)

s= "ABC" S1=s.center (5, ' * ') print (S1) results are *abc*

Common operations for data type &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.