Introduction to Python basic data types

Source: Internet
Author: User
                                              Python3 the variables in the base data type Python3 do not need to be declared.   Each variable must be assigned before it is used, and the variable will not be created until the variable is assigned. There are 6 standard data types in Python3: number (numeric), string, list, tuple (tuple), Dictionary: (Dict), set (sets) Number: Python3 support Int,float,   The Bool,complex (plural) type () function can view the object type that the variable refers to string string: The string in Python is enclosed in single quotation marks (') or double quotation marks ("), and the special characters are escaped with a backslash (\). Note: ' ... ' is not particularly useful when creating short strings, mainly for creating multiline strings, as in the following example: >>> poem = ' There was a young Lady of Norway, ...   Who casually sat in a doorway; ... When the door squeezed her flat, ...   She exclaimed, "What's that?" ... This courageous young Lady of Norway. Python also allows empty strings to exist, without any characters, to be perfectly legal! Conversions between numbers and strings: strings converted to numbers: >>> int (' 20 ') >>> float (' 20.0 ') >>> Int () 20 numbers converted to words String: >>> str (+) ' >>> str (20.0) ' 20.0 ' >>> str (TRUE) ' true ' use + stitching in Python to try +   Splicing multiple strings or string variables >>> ' Release the tom! ' + ' at once! ' Release the tom! At once! uses the [] extract character to add [] after the string, and to add an offset in [] to remove theString.   The first character has an offset of 0, the second is 1, and so on. The first offset on the right is-1, the second is-2, from right to left, and so on ... >>> str = ' abcdefghijklmnopqrstuvwxyz ' >>> str[0] ' a ' >>&gt ;   STR[-1] ' z ' >>> str[3] ' d ' strings are immutable, sometimes changing strings, you need to combine some string functions: replace (), and shard operations >>> name = ' Henny ' >>> name.replace (' H ', ' P ') ' Penny ' >>> ' P ' + name[1:] ' Penny ' uses [Start:end:step] Shard: Shard operation: Can be from a string Extracts substrings in the Start offset start, end offset end, and optional step step to define a shard 1. [:] Extracts the entire string from the beginning to the end of 2. [Start:] Extract from start to end 3. [: end] Extracts from the beginning to end-1 4. [Start:end] Extract from start to end-1 end 5. [Start:end:step] extracted from start to end-1, each step extracts a character >>> str = ' QWERTYUIOP ' >>> str[:] ' qwertyuiop ' >> > str[5:] ' yuiop ' >>> str[-3:] ' IOP ' >>> str[:-3] ' Qwertyu ' >>> str[-6:-3] ' ty U ' Step 3 >>> str[::3] ' qrup ' using slice reverse output string >>> str[::-1] ' poiuytrewq ' string other common operations: >>> str = ' QWERTYUIOP ' computes the length of the string >>> len (str) 10 using split () split: Use the built-in string function SPLIt () can split a string into a list of several substrings based on a delimiter.   A list is a series of values that are separated by commas between values and values, and the entire list is wrapped in square brackets. >>> todos = ' Get gloves,get mask,give cat Vitamins,call Ambulance ' >>> todos.split (', ') [' Get Gloves ' Get mask ', ' Give cat vitamins ', ' call ambulance '] in the above example, the string name is Todos, the function name is split (), the passed argument is a single delimiter split (), the passed argument is a single delimiter ',   '。   If you do not specify a delimiter, split () will default to white space characters--line breaks, spaces, tabs. >>> todos.split () [' Get ', ' gloves,get ', ' mask,give ', ' cat ', ' vitamins,call ', ' ambulance '] use Join () Merge: Maybe you already guessed it. , the join () function is exactly the opposite of the split () function: It will contain a list of substrings of the decomposition and synthesize the substrings into a full large string. Join () >>> crypto_list = [' Yeti ', ' Bigfoot ', ' Loch Ness Monster '] >>> crypto_string = ', '. Join (CRYP  To_list) >>> print (' Found and signing book deals: ', crypto_string) Found and signing book Deals:yeti, Bigfoot, Loch Ness Monster Replace with replace (): >>> str = ' QWERTYUIOP ' >>> str.replace (' W ', ' ooooo ') ' Qoooooertyui Op ' up to 3 changes >>> str = ' QWERWERWERWTYTEWWIITW ' >>>Str.replace (' W ', ' oooo ', 3) ' QOOOOEROOOOEROOOOERWTYTEWWIITW ' computes the number of occurrences of ' W ' in the string >>> str.count (' W ') 7 
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.