Basic knowledge of strings and lists in Python programming

Source: Internet
Author: User
Python String
The string is the most commonly used data type in Python. We can use quotation marks to create strings.
Creating a string is simple, as long as you assign a value to the variable. For example:

var1 = ' Hello world! ' VAR2 = "Python Programming"


Python accesses a value in a string
Python does not support single character types, and one character is also used as a string in Python.
Python accesses substrings, you can use square brackets to intercept strings, as in the following example:

#!/usr/bin/pythonvar1 = ' Hello world! ' VAR2 = "Python Programming" print "var1[0]:", Var1[0]print "Var2[1:5]:", Var2[1:5]

The result of the above instance execution:

VAR1[0]: Hvar2[1:5]: Ytho

Python string update
You can modify the existing string and assign it to another variable, as in the following example:

#!/usr/bin/pythonvar1 = ' Hello world! ' Print "Updated String:-", var1[:6] + ' Python '


Result of the above instance execution

Updated String:-Hello Python

Python List (Lists)
The sequence is the most basic data structure in Python. Each element in the sequence is assigned a number-its position, or index, the first index is 0, the second index is 1, and so on.
Python has 6 built-in types of sequences, but the most common are lists and tuples.
Sequences can be performed by operations including indexing, slicing, adding, multiplying, and checking members.
In addition, Python has built-in methods for determining the length of a sequence and determining the maximum and minimum elements.
A list is the most commonly used Python data type and can appear as a comma-separated value within a square bracket.
Data items for a list do not need to have the same type
Create a list by enclosing separate data items separated by commas in square brackets. As shown below:

List1 = [' Physics ', ' Chemistry ', 1997, 2000];list2 = [1, 2, 3, 4, 5];list3 = ["A", "B", "C", "D"];

As with the index of a string, the list index starts at 0. Lists can be intercepted, combined, and so on.
Accessing values in a list
Use the subscript index to access the values in the list, and you can also use square brackets to intercept the characters as follows:

#!/usr/bin/pythonlist1 = [' Physics ', ' Chemistry ', 1997, 2000];list2 = [1, 2, 3, 4, 5, 6, 7];p rint "list1[0]:", list1[0]p Rint "List2[1:5]:", List2[1:5]

The result of the above example output:

LIST1[0]: Physicslist2[1:5]: [2, 3, 4, 5]

Update list
You can modify or update the list's data items, or you can use the Append () method to add the list items as follows:

#!/usr/bin/pythonlist = [' Physics ', ' Chemistry ', 1997,];p rint "Value available at index 2:" Print list[2];list[2] = 2001;print "New value available at index 2:" Print list[2];

Note: We will discuss the use of the Append () method in the following chapters
The result of the above example output:

Value available at index 2:1997new value available at index 2:2001

To delete a list element
You can use the DEL statement to delete the elements of a list, as in the following example:

#!/usr/bin/pythonlist1 = [' Physics ', ' Chemistry ', 1997,];p rint list1;del list1[2];p rint "after deleting value at Inde X 2: "Print list1;

The result of the above example output:

[' Physics ', ' Chemistry ', 1997, 2000] After deleting value at index 2: [' Physics ', ' chemistry ', 2000]
  • 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.