Basic knowledge about strings and lists in Python Programming

Source: Internet
Author: User
This article mainly introduces the basic knowledge of strings and lists in Python programming. it is the basic knowledge of getting started with Python. For more information, see Python string
A string is the most common data type in Python. We can use quotation marks to create strings.
It is easy to create a string by assigning a value to the variable. For example:

var1 = 'Hello World!'var2 = "Python Programming"


Value in the Python access string
Python does not support the single character type. a single character is also used as a string in Python.
Python can use square brackets to intercept a substring, as shown in the following example:

#!/usr/bin/pythonvar1 = 'Hello World!'var2 = "Python Programming"print "var1[0]: ", var1[0]print "var2[1:5]: ", var2[1:5]

Execution result of the above instance:

var1[0]: Hvar2[1:5]: ytho

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

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


Execution result of the above instance

Updated String :- Hello Python

Python list (Lists)
Sequence is the most basic data structure in Python. Each element in the sequence is assigned a number-its location, or index. The first index is 0, the second index is 1, and so on.
Python has six built-in sequence types, but the most common is list and metadata.
Operations that can be performed by sequences include indexing, slicing, adding, multiplication, and checking members.
In addition, Python has built-in methods to determine the sequence length and the maximum and minimum elements.
A list is the most common Python data type. it can appear as a comma-separated value in square brackets.
List data items do not need to have the same type
To create a list, you only need to enclose different data items separated by commas (,) in square brackets. As follows:

list1 = ['physics', 'chemistry', 1997, 2000];list2 = [1, 2, 3, 4, 5 ];list3 = ["a", "b", "c", "d"];

Like the string index, the list index starts from 0. The list can be intercepted and combined.
Value in the access list
Use subscript indexes to access values in the list. you can also use square brackets to intercept characters, as shown below:

#!/usr/bin/pythonlist1 = ['physics', 'chemistry', 1997, 2000];list2 = [1, 2, 3, 4, 5, 6, 7 ];print "list1[0]: ", list1[0]print "list2[1:5]: ", list2[1:5]

Output result of the above instance:

list1[0]: physicslist2[1:5]: [2, 3, 4, 5]

Update list
You can modify or update the data items in the list, or use the append () method to add the list items, as shown below:

#!/usr/bin/pythonlist = ['physics', 'chemistry', 1997, 2000];print "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 how to use the append () method in the following sections.
Output result of the above instance:

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

Delete list elements
You can use the del statement to delete the list elements, as shown in the following example:

#!/usr/bin/pythonlist1 = ['physics', 'chemistry', 1997, 2000];print list1;del list1[2];print "After deleting value at index 2 : "print list1;

Output result of the above instance:

['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.