python- string concatenation string concatenation of three methods of three methods python-

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.

Copy the code
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)
Copy the code

The output results are as follows

---info ofwendy---
name:wendy
age:26
job:it
salary:8000

2. Use splicing%

Copy the code
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)
Copy the code

When the digital type, format conversion remember

Copy the code
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)
Copy the code

Output

Copy the code
name:llll
age:89
<class 'int'>
job:89
salary:1000

---info of llll---
name: llll
age: 89
job: 89
salary: 1000
Copy the code

3 format using the format

(1)

Copy the code
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)
Copy the code

Output Format

Copy the code
name:qwww
age:12
job:ty
salary:1348

---info of qwww---
name:qwww
age:12
job:ty
salary:1348
Copy the code

(2)

Copy the code
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)
Copy the code

Output

Copy the code
name:liyang
age:67
job:teacher
salary:5000

---info of liyang---
name:liyang
age:67
job:teacher
salary:5000
Copy the code

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.

Copy the code
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)
Copy the code

The output results are as follows

---info ofwendy---
name:wendy
age:26
job:it
salary:8000

2. Use splicing%

Copy the code
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)
Copy the code

When the digital type, format conversion remember

Copy the code
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)
Copy the code

Output

Copy the code
name:llll
age:89
<class 'int'>
job:89
salary:1000

---info of llll---
name: llll
age: 89
job: 89
salary: 1000
Copy the code

3 format using the format

(1)

Copy the code
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)
Copy the code

Output Format

Copy the code
name:qwww
age:12
job:ty
salary:1348

---info of qwww---
name:qwww
age:12
job:ty
salary:1348
Copy the code

(2)

Copy the code
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)
Copy the code

Output

Copy the code
name:liyang
age:67
job:teacher
salary:5000

---info of liyang---
name:liyang
age:67
job:teacher
salary:5000
Copy the code

Guess you like

Origin www.cnblogs.com/wz123/p/11910863.html