1.使用加号(+)号进行拼接
字符串拼接直接进行相加就可以,比较容易理解,但是一定要记得,变量直接相加,不是变量就要用引号引起来,不然会出错,另外数字是要转换为字符串才能够进行相加的,这点一定要记住,不能把数字直接相加。
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)
输出结果如下
---info ofwendy--- name:wendy age:26 job:it salary:8000
2.使用%进行拼接
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)
当有数字类型,记得转换格式
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)
输出结果
name:llll age:89 <class ‘int‘> job:89 salary:1000 ---info of llll--- name: llll age: 89 job: 89 salary: 1000
3 使用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)
输出格式
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)
输出结果
name:liyang age:67 job:teacher salary:5000 ---info of liyang--- name:liyang age:67 job:teacher salary:5000