How to add elements to a string in python

(*-*)浩
Release: 2019-07-09 10:17:15
Original
7931 people have browsed it

The string object in Python cannot be changed, that is, it is not possible to directly modify one or more characters in the string. That is, the string object in Python cannot be changed, but the reference of the string object can Changed to repoint to a new string object.

How to add elements to a string in python

Direct elements outside the string (recommended learning: Python video tutorial)

name = 'zheng'
print('my name is '+name)
Copy after login

% Direct string outside % (element)

A string formatting syntax. The basic usage is to insert the value into the string of the %s placeholder.

%s, means formatting an object as characters

name = 'zhang'
age = '25'
print('my name is %s'%(name)+' my age is %d'%(age))
Copy after login

format() function

When more elements are to be inserted

name = input('请输入你的名字:')
    data = input('请输入发送内容:')
    dest_ip = input('请输入ip:')
    dest_port = 2425
    chat_socket.sendto('1:123456:发送者的名称:{my_name}:32:{my_data}'.format(my_name=name,my_data=data).encode('gbk'),(dest_ip,dest_port))
Copy after login

Format() directly, and slowly assign values ​​to it later

For more Python-related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of How to add elements to a string in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!