Python strings, lists, dictionaries, and functions

Source: Internet
Author: User
Tags php explode

One, the string
    • In Python, the string does not need to be decomposed by a split in PHP explode or JavaScript, which can be used to get each character in a string directly from the subscript, starting from 0, if signed from a very long-expected, subscript starting from 1
str=‘abcde‘print(str[0])   #结果为:aprint(str[2])   #结果为:c
    • The length of a string can be obtained through the len () function

    • Slice: str[start position: End Position: Step]

    • If you want to take a few characters (slices) in the string, you can add a colon solid line in the square brackets, a small start before the colon, a colon after the end of the subscript, intercept the colon before the beginning of the colon between the character, the tail does not pinch the head

str=‘abcde‘print(str[1:3])     #结果为:bcprint(str[2:3])     #结果为:cprint(str[1:0])     #结果为:bcde  冒号后为零,视为取到最后一个
    • After the second colon , the number after the second colon is equal to the step, which is the subscript plus a few
Str = ' ABCDEABCDE 'Print(Str[1:8:2])#结果为: BdacPrint(Str[-1:0:-1])#结果为: EDCBAEDCBPrint(Str[-1::-1])#结果为: EDCBAEDCBAPrint(Str[::-1])#结果为: EDCBAEDCBAPrint(Str[::1])#结果为: abcedabced
  • Common String Operations:
  • Find (): Finds the first occurrence of a string in the target string from left to right, and cannot find return-1.

    str=‘hello world!‘print(str.find(‘o‘))    #结果为:4
  • RFind (): Opposite to find, find from right to left.

  • Index (): The function is the same as find, and the difference is that if the index is not found, an exception is thrown.

      • Rindex (): similarly.

      • COUNT (): Returns the number of occurrences of the lookup string in the target string, with no return 0.

      • Replace (' target string ', ' string to replace ', replaced several times)

      • Split (): With the Split function in JavaScript, if nothing is written in parentheses, the default is to cut the object with a space (very useful)

      • Join (): works with the Join function in JavaScript, but in a different form.

    = [‘aaa‘‘bbb‘‘ccc‘=‘#‘print(b.join(a))            #结果为:aaa#bbb#ccc
    • Capitalize (): Capitalize the first character of the string

    • Title (): Capitalize the first letter of each word of the string

    • StartsWith (): Checks whether the string starts with the target string

    • EndsWith (): Contrary to StartsWith

    • Lower (): lowercase all uppercase characters in the string

    • Upper (): Contrary to lower

    • Ljust (), Rjust (), center (): Within the length of the number filled in parentheses, left, right, center.

    • Lstrip (), Rstrip (), strip (): Trim with JavaScript

    • Partition (): The string is used to center the target string, divided into left and right three parts, return a tuple.

      str=‘hello world‘print(str.partition(‘o‘))   #结果为:(‘hell‘, ‘o‘, ‘ world‘)
    • Rpartition (): function with partition, take first from right to left.

    • Splitliness (): Split by line break.

    • Isalpha (): Determines if the target string is not a letter.

    • IsDigit (): Judging if the target is not a number.

    • Isalnum (): The goal is to determine whether there are both letters and numbers.

    • Isspace (): Determine if the target is not a space

Second, List
    • Form:
= [‘张三‘‘李四‘‘王五‘]
  • Additions to the list:

  • Append ("What to add")
  • Insert ("Location", "What to add")

  • Merge list:

  • Merge directly with the + sign

  • Using the Extend () function

    = [‘张三‘‘李四‘"王五"= [‘赵六‘‘吴七‘=+ names2print(names3)                   #结果为:[‘张三‘, ‘李四‘, ‘王五‘, ‘赵六‘, ‘吴七‘]names1.extend(names2)print(names1)                   #结果为:[‘张三‘, ‘李四‘, ‘王五‘, ‘赵六‘, ‘吴七‘]
  • Deletion of the list:
  • Pop (): The last one in the pop-up list
  • Remove (content): Deletes the first target content in the list
  • del list name [subscript]

  • Lookup of the list:
  • "What to look for" in list name
  • "What to look for" not in list name

  • Traversal of the list:

forin 列表名:    print(temp)else:    print(‘end‘)    # for循环加上else,即为在for循环执行完成后,执行else部分
Third, the dictionary
    • Form: dic = {key: value, Key: Value ...}, which is equivalent to an associative array in PHP, behaves in a form similar to a JSON object
    • Deletion of the Dictionary del dictionary name [' Key Name ']
    • Gets the data in the dictionary with get, and does not error when the corresponding content does not exist: dictionary name. Get (' Key name ')
    • Functions of the Dictionary:
    • Len (dic name): Gets the number of key-value pairs for the dictionary.
    • Dic.keys (): Returns all keys in the dictionary.
    • Dic.values (): Returns all values in the dictionary.
    • Dic.items (): Converts the dictionary to Ganso.
Iv. Ganso
    • (11,22,33) function is the same as the list, and differs in that Yuanju cannot be modified after it is defined.
Five, function
    • Indefinite length function (Ganso):

      def*args):    print(a)    print(b)    print(args)example(12345678)    #结果为:1 2 (3, 4, 5, 6, 7)
    • Indefinite length parameter (dictionary):

      def test(a, b, c = 33, *args, **kwargs):    print(a)    print(b)    print(c)    print(args)    print(kwargs)test(11, 22, 33, task = 99, done = 89)    #结果为:11 22 33 () {‘task‘: 99, ‘done‘: 89}

Python strings, lists, dictionaries, and functions

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.