Python Basics (string)

Source: Internet
Author: User
Tags string format

In the process of learning the basic Python tutorial, we will generally deal with the basics of strings, lists, tuples, dictionaries and so on, and then we'll take a look at the examples of strings, lists, tuples, and dictionaries as Follows:

one, the String:

The string is the most commonly used data type in Python. We can use quotation marks (' or ') to create a string. Creating a string is simple, as long as you assign a value to the Variable. For example:

var1= "Big data" var2= ' OpenStack ' need to be aware that in python, double quotation marks ("") are exactly the same as single quotes (""), and there is no problem of inconsistent single and double quotes in the Shell.

Operations involving Strings:

1. Concatenation of strings

In some special cases we need to splice strings, use the + number to achieve stitching, need to pay attention to the following matters:

The string in Python is represented in the C language as a character array, and each time a string is created, it needs to open a contiguous space in memory, and once you need to modify the string, you need to open up again, and the Evil + sign will re-open up a space within each occurrence. So try to use less string Concatenation.

A simple example:

#!/usr/bin/env python#_*_coding:utf-8_*_print ('----* * * String formatted 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 formatted output string concatenation method * * *----' "" "" "" "string concatenation of all evils string concatenation: the string in Python is embodied in the C language as a character array, each time you create a string, you need to open a contiguous space in memory, and once you need to modify the string, you need to open up again, Every occurrence of the evil + sign will re-open a space within. "" "info="---info of ' + name + '---name: ' + ' + ' + ' ' age: ' + ' + ' + ' ' job: ' + job + ' ' salary: "+ Salary +" "print (info)

Results of the Output:

----* * * String Formatted OUTPUT * * *----input your name:pythoninput your age:20input your job:linux sainput your salary:20000----* * * string format output string concatenation method * * *-------info of Python---name:pythonage:20job:linux Sasalary:20000process finished with exit code 0

As a result, we can see a string concatenation to implement the format of the string method, and then we share the other way of string formatting.

#!/usr/bin/env python#_*_coding:utf-8_*_print ('----* * * * String formatted output * *----') name= input ("input your Name:") age= input (" Input your age: ") job= input (" input your job: ") salary= input (" input your salary: ") print ('----* * * String Formatted 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 ('----* * * String formatted output format can be without parameters but suitable for fewer parameters * * *----')

Execution Result:

Input your name:baiduinput your age:22input your job:99input your salary:44----* * * String Formatted OUTPUT * * *----name:baiduage:22job:9 9salary:44----* * * String formatted output format can be without parameters but suitable for less parameters * * *----Process finished with exit code 0

As a result, we see the random input of the numbers and letters and other different content is normal output, and age should be expressed as integers, the format of the output process, we can not only use the%s can also use%d and%f, (the string is%s; integer%d; floating-point%f).

besides, There are other formats, such as

#!/usr/bin/env python#_*_coding:utf-8_*_#!/usr/bin/env python#_*_coding:utf-8_*_print ('----* * * String Formatted 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} The way to fit the variable is that the parentheses are 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 way fits a variable less case parentheses are curly braces-------****** Name:linuxage:mysql Dbajob:mysql Dbasalary:30000process finished with exit code 0

String is our daily use of the most data type, there is a lot of knowledge, and so on later semester process to continue to Improve.



Python Basics (string)

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.