Python Learning Note Five: string common operations, dictionaries, three-level menu instances

Source: Internet
Author: User

String Common operations

7 Month Day, 7 Month Day , 7 Month Day, 7 Month Day, 8 Month Day, 2 Month Day

Initial capital: A_str.capitalize ()

Number of statistics strings: A_str.count ("x")

Output character, not enough to fill with the specified character, the center of the character: A_str.center (50, "-")

Determine what the string ends with: A_str.endwith ("XX")

Converts the tab in a string to a specified number of spaces: A_str.expandtabs (tabsize=30)

Finds the position of the specified string in the string: A_str.find ("XX")

The string can also be sliced: A_str[a_str.find ("XX"):]

Formatted output: A_str.format (xx= "x", xxx= "xxx")

Dictionary format output: a_str.format_map ({' name ': ' Alex ', ' Year ': 12})

Determine if it is an Arabic numeral (all English characters +0-9): A_str.isalnum ()

Determine if it is an English character: A_str.isalpha ()

Determine whether it is a decimal number: A_str.isdecimal ()???

Determine whether it is an integer: A_str.isdigit ()

Determine if the identifier is valid (valid variable name): A_str.isidentifier ()

To determine if it is lowercase: a_str.islower ()

Determine if there are only numbers (no decimal points): A_str.isnumeric (), test: "0132". IsNumeric ()

Determine if it is a space: A_str.ispace ()

Determine if the first letter of each word is capitalized: A_str.istitle ()

Determine if it can be printed: a_str.isprintable () #tty file, drive file

To determine if uppercase: A_str.isupper ()

Combines a string list into a string, separated by the specified character: "Specified character". Join (A_str_list)

Outputs a string of the specified length, the string on the left, and insufficient when the specified character (string? ) padding: a_str.ljust (50, ' * ')

Outputs a string of the specified length, the string on the right, and the specified character when insufficient (string? ) padding: a_str.rjust (50, ' * ')

Character lowercase: a_str.lower ()

Character capitalization: A_str.upper ()

Remove the left space carriage return: "Test". Lstrip ()

Remove the right space carriage return: "Test". Rstrip ()

Remove the space on both sides to enter: ' test\n '. Strip () \ r will be?

Converts the specified letter to the corresponding number:

P=str.maketrans ("abcdef", "123456")

Print ("Alex Li". Translate (p))

Replacement string: A_str.replace (' l ', ' l ', 1)

Find character position in string from right to left: A_str.rfind (' x ')

Separates a string into a list by a specified character, without specifying a space: A_str.split ()

Separate strings by carriage return as a list: A_str.splitlines () (\r,\n?). )

Case conversion: A_str.swapcase ()

Fill in 0 of the string with insufficient parts: A_str.zfill (50)

Dictionary

7.18,7.19,7.21,7.28,8.28,2.28

Key-value

JSON format

Unordered, key is unique, the first example has a curly brace at the end

Value, Info[key],info.get (key)

Determine if key exists, key in Info,python2:info.haskey (key)

Multi-level dictionary nesting, modification, primary level positioning, assignment

Key do not use Chinese

Print all Values,keys,setdefault (), first determine if there is??? , does not exist then creates

Info.update (Info2), merge two dictionaries, key consistent when overwriting old values with new values???

Convert to list, info. Items ()

Fromkeys (alist,value) Create a new dictionary, each value in the list is only key???

Output Key,value:

For key in info:

Print (I,info[i])--efficient

For k,v in Info.items ():

Print (K,V)--low efficiency, first converted to list after output

Level Three Menu instance

Flow chart

Building a dictionary

Cycle

Pass:nothing, I don't mean to do anything.

Homework:

Shopping cart optimization, divided into sellers and buyers two documents

Buyer: The product information exists in the file, each run shows the purchased goods and balances

Sellers: Can add items, modify prices

Python Learning Note Five: string common operations, dictionaries, three-level menu instances

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.