This article introduces the basic knowledge of python. in the course of the basic python tutorial, the basic knowledge of strings, lists, tuples, and dictionaries is generally involved, next, we will record the cases of learning strings, lists, tuples, and dictionaries as follows:
1. string:
A string is the most common data type in Python. We can use quotation marks ('or ") to create strings. It is easy to create a string by assigning a value to the variable. For example:
Var1 = "Big data" var2 = 'openstack' note that double quotation marks ("") and single quotation marks ('') in python have exactly the same meaning, there is no inconsistency between single quotes and double quotes in Shell.
Operations involving strings:
1. string concatenation
In some special cases, we need to splice strings and use the plus sign (+) to splice them. Note the following:
The string in python is embodied in the C language as a character array. each time you create a string, you need to open up a continuous empty block in the memory. once you need to modify the string, you need to open up space again, every appearance of the "+" symbol of the evil "will open up a space from it, so try to splice as few strings as possible.
Simple example:
#! /Usr/bin/env python # _ * _ coding: UTF-8 _ * _ print ('---- *** formatted string output ***----') "name = input (" input your name: ") password = input (" inpur your password: ") print (" your name: % s \ nyour password: % s "% (name, password)" name = input ("input your name:") age = input ("input your age :") job = input ("input your job:") salary = input ("input your salary :") print ('---- *** string formatting output string concatenation method *** ----') "string concatenation method: the string in python is embodied in the C language. It is a character array. each time you create a string, you need to open up a continuous null field in the memory. once you need to modify the string, you need to open up space again, every time the "+" icon appears, it will open up a new space. "Info = ''' --- info of ''' + name + ''' --- name: ''' + name + ''' age: ''' + age + ''' job: ''' + job + ''' salary: ''' + salary + ''' print (info)
Output result:
---- *** Formatted string output *** ---- input your name: pythoninput your age: 20 input your job: linux sainput your salary: 20000 ---- *** string formatting output string concatenation method *** ---- --- info of python --- name: pythonage: 20job: linux sasalary: 20000 Process finished with exit code 0
As a result, we can see a string concatenation method to format strings. next we will share other string formatting methods.
#! /Usr/bin/env python # _ * _ coding: UTF-8 _ * _ print ('---- *** formatted string output ***----') name = input ("input your name:") age = input ("input your age:") job = input ("input your job :") salary = input ("input your salary:") print ('---- *** formatted string output *** ----') # print ('name: % s \ nage: % d \ njob: % s \ nsalary: % s' % (name, int (age), job, salary) print ("name: % sage: % djob: % ssalary: % s "% (name, int (age), job, salary )) print ('---- *** formatted string output format can be without parameters, but it is suitable for a few parameters ***----')
Execution result:
Input your name: baiduinput your age: 22 input your job: 99 input your salary: 44 ---- *** formatted string output *** ---- name: baiduage: 22job: 99 salary: 44 ---- *** formatted string output format can be without parameters, but it is suitable for few parameters. *** ---- Process finished with exit code 0
As a result, we can see that the numbers and letters entered at will are normally output, and age should be represented by integers. during the formatting process, we can use both % s and % d and % f (the string is % s; integer % d; floating point number % f ).
There are other formats, such
#! /Usr/bin/env python # _ * _ coding: UTF-8 _*_#! /Usr/bin/env python # _ * _ coding: UTF-8 _ * _ print ('---- *** formatted string output ***----') name = input ("input your name:") age = input ("input your age:") job = input ("input your job :") salary = input ("input your salary:") info3 = "------- ******* info of {_ name} ------- ******* name: {_ name} age: {_ age} job: {_ job} salary: {_ salary }""". format (_ name = name, _ age = age, _ job = job, _ salary = salary) print (info3) info4 = "" ------- ******* info of {0} this method is applicable when there are few variables. curly braces ------- ******* name: {0} age: {1} job: {2} salary: {3 }""". format (name, age, job, salary) print (info4)
Execution result:
Input your name: linuxinput your age: mysql DBAinput your job: mysql DBAinput your salary: 30000 ------- ****** info of linux ------- ****** name: linuxage: mysql DBAjob: mysql DBAsalary: 30000 ------- ******* info of linux this method is suitable for the case where there are few variables. curly brackets ------- ******* name: linuxage: mysql DBAjob: mysql DBAsalary: 30000 Process finished with exit code 0
String is the most commonly used data type. we still have a lot of knowledge and will continue to improve it later in the semester.
The above is the summary of the basic knowledge string of python. For more information, see other related articles in the first PHP community!