python- string concatenation of three methods
1. Use number plus sign (+) for splicing
String concatenation direct sum can be relatively easy to understand, but we must remember, by adding variables directly, not a variable must be quoted, or be wrong, the other numbers are to be converted to a string to be able to be summed and this is important to remember that the numbers can not be added up.
name = input("name:") age = input("age:") job = input("job:") salary = input("salary:") info =''' ---info of '''+name+ '''--- name:'''+name+''' age:'''+age+''' job:'''+job+''' salary:'''+salary+''' ''' print(info)
The output results are as follows
---info ofwendy--- name:wendy age:26 job:it salary:8000
2. Use splicing%
name = input("name:") age = input("age:") job = input("job:") salary = input("salary:") info =''' ---info of %s--- name: %s age: %s job: %s salary: %s '''%(name,name,age,job,salary) print(info)
When the digital type, format conversion remember
name = input("name:") age = int(input("age:")) #integer print(type(age)) job = input("job:") salary = input("salary:") info =''' ---info of %s--- name: %s age: %d job: %s salary: %s '''%(name,name,age,job,salary) print(info)
Output
name:llll age:89 <class 'int'> job:89 salary:1000 ---info of llll--- name: llll age: 89 job: 89 salary: 1000
3 format using the format
(1)
name=input("name:") age=input("age:") job=input("job:") salary=input("salary:") info2 =''' ---info of {name}--- name:{name} age:{age} job:{job} salary:{salary} '''.format(name=name, age=age, job=job, salary=salary) print(info2)
Output Format
name:qwww age:12 job:ty salary:1348 ---info of qwww--- name:qwww age:12 job:ty salary:1348
(2)
name=input("name:") age=input("age:") job=input("job:") salary=input("salary:") info2 =''' ---info of {0}--- name:{0} age:{1} job:{2} salary:{3} '''.format(name,age,job,salary) print(info2)
Output
name:liyang age:67 job:teacher salary:5000 ---info of liyang--- name:liyang age:67 job:teacher salary:5000
1. Use number plus sign (+) for splicing
String concatenation direct sum can be relatively easy to understand, but we must remember, by adding variables directly, not a variable must be quoted, or be wrong, the other numbers are to be converted to a string to be able to be summed and this is important to remember that the numbers can not be added up.
name = input("name:") age = input("age:") job = input("job:") salary = input("salary:") info =''' ---info of '''+name+ '''--- name:'''+name+''' age:'''+age+''' job:'''+job+''' salary:'''+salary+''' ''' print(info)
The output results are as follows
---info ofwendy--- name:wendy age:26 job:it salary:8000
2. Use splicing%
name = input("name:") age = input("age:") job = input("job:") salary = input("salary:") info =''' ---info of %s--- name: %s age: %s job: %s salary: %s '''%(name,name,age,job,salary) print(info)
When the digital type, format conversion remember
name = input("name:") age = int(input("age:")) #integer print(type(age)) job = input("job:") salary = input("salary:") info =''' ---info of %s--- name: %s age: %d job: %s salary: %s '''%(name,name,age,job,salary) print(info)
Output
name:llll age:89 <class 'int'> job:89 salary:1000 ---info of llll--- name: llll age: 89 job: 89 salary: 1000
3 format using the format
(1)
name=input("name:") age=input("age:") job=input("job:") salary=input("salary:") info2 =''' ---info of {name}--- name:{name} age:{age} job:{job} salary:{salary} '''.format(name=name, age=age, job=job, salary=salary) print(info2)
Output Format
name:qwww age:12 job:ty salary:1348 ---info of qwww--- name:qwww age:12 job:ty salary:1348
(2)
name=input("name:") age=input("age:") job=input("job:") salary=input("salary:") info2 =''' ---info of {0}--- name:{0} age:{1} job:{2} salary:{3} '''.format(name,age,job,salary) print(info2)
Output
name:liyang age:67 job:teacher salary:5000 ---info of liyang--- name:liyang age:67 job:teacher salary:5000