Introduction to Basic Python data types

Source: Internet
Author: User
Variables in python3, the basic data type, do not need to be declared. Each variable must be assigned a value before it can be used. Python3 has six standard data types: Number, String, list, Tuple, dictionary (Dict), and set)
Variables in python3, the basic data type, do not need to be declared. Each variable must be assigned a value before it can be used. Python3 has six standard data types: Number, String, list, Tuple, dictionary (Dict), and set) number (Number): Python3 supports the int, float, bool, and complex (plural) type () functions to view the object type specified by the variable String (String ): strings in Python are enclosed by single quotation marks (') or double quotation marks ("), and special characters are escaped using a backslash. Note :'''... ''' three-element quotation marks are not particularly useful when a short string is created. They are mainly used to create multi-line strings, for example: >>> poem = ''' There was a Young Lady of Norway ,... who casually sat in a doorway ;... when the door squeezed her flat ,... she exclaimed, "What of that? "... This courageous Young Lady of Norway. ''' Python also allows null strings and does not contain any characters. it is completely legal! Conversion between numbers and strings: convert a string to a number: >>> int ('20') 20 >>> float ('20') 20.0 >>> int (20) 20 digits to string: >>> str (20) '20' >>> str (20.0) '20. 0' >>> str (True) 'true' use + concatenate in python to try + concatenate multiple strings or string variables >>> 'release the Tom! '+' At once! 'Release the Tom! At once! Use [] extract character to add [] to the end of the string, and add an offset to [] to retrieve the string at this position. The offset of the first character is 0, the second is 1, and so on. The first offset on the right is-1, the second offset is-2, and so on from right to left. str = 'abcdefghijklmnopqrstuvwxy'> str [0] 'a'> str [-1] 'Z' >>>> str [3] 'D' string is immutable, sometimes, to change the string, you need to combine some string functions: replace () and sharding operations >>> name = 'henny '>>> name. replace ('H', 'P') 'Penny '>>> 'P' + name [1:] 'Penny' use [start: end: step] for sharding: sharding: you can extract substrings from a string. Start offset, end offset, and optional step to define a shard 1. [:] extract the entire string from the beginning to the end. 2. [start:] extract from start to end 3. [: end] extract from the beginning to end-1 4. [start: end] extract from start to end of end-1 5. [start: end: step] extracts one character from start to end-1, and each step extracts one character >>> str = 'qwertyuiop' >>> str [:] 'qwertyuiop' >>> str [5:] 'yuiop' >>> str [-3:] 'iop' >>>> str [: -3] 'qwertyu' >>> str [-6:-3] the step size of 'tyu' is 3 >>> str [:: 3] 'qrule' uses the slice reverse output string >>> str [:-1] 'pouytrewq' other common operations: >>> str = 'qwertyuiop' Calculate the length of a string >>> len (str) 10 split by split (): Use the built-in string function split () strings can be divided into a list composed of several substrings based on delimiters. A list is a series of values separated by commas (,). The list is enclosed by 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 preceding example, the string name is todos, the function name is split (). the input parameter is a single separator split (), and the input parameter is a single separator ','. If no delimiter is specified, split () uses the blank characters by default-line breaks, spaces, and tabs. >>> Todos. split () ['GET', 'gloves, get', 'mask, Give', 'cat', 'Vitamins, call', 'Ambulance '] merge using join: as you may have guessed, the join () function is the opposite of the split () function: it splits the list containing several substrings, and combine these substrings into a complete large string. Join () >>> crypto_list = ['eti', 'Bigfoot', 'Loch Ness monster'] >>> crypto_string = ','. join (crypto_list) >>> print ('found and signing book deals: ', crypto_string) Found and signing book deals: Yeti, Bigfoot, Loch Ness Monster use replace () replace: >>> str = 'qwertyuiop' >>> str. replace ('W', 'ooooo') 'qoooooertyuiop' can be modified at most 3 places >>> str = 'qwerwerwtytewwiitw' >>> str. replace ('W', 'Oooo ', 3) 'qooooerooerooooerwtytewwiitw' calculates the number of times 'W' appears in the string >>> str. count ('w') 7

The above is a detailed description of the basic Python data types. For more information, see other related articles in the first PHP community!

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.