Home>Article>Backend Development> How to add elements to a string in python

How to add elements to a string in python

(*-*)浩
(*-*)浩 Original
2019-06-28 11:04:31 8018browse

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)

% 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))

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))

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

For more Python-related technical articles, please visit thePython Tutorialcolumn 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!

Statement:
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