程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python string splicing

編輯:Python

List of articles

  • Python character string
        • 【 Example 】 Concatenate strings in continuous writing :
        • Running results :
        • 【 Example 】 Use + Operators concatenate strings :
        • Running results :
    • Python String and number splicing
        • Look at the code below :
        • Running results :
      • str() and repr() The difference between
        • Please see the following example :
        • Running results :


Python character string

stay Python Middle splicing ( Connect ) Strings are very simple , You can write two strings next to each other , The specific format is :

strname = "str1" "str2"

strname Represents the string variable name after splicing ,str1 and str2 Is the string content to be spliced . Use this way of writing ,Python Will automatically splice two strings together .

【 Example 】 Concatenate strings in continuous writing :

str1 = "Python course " "http://c.biancheng.net/python/"
print(str1)
str2 = "Java" "Python" "C++" "PHP"
print(str2)

Running results :

Python course http://c.biancheng.net/python/
JavaPythonC++PHP

It should be noted that , This method can only concatenate string constants .

If you need to use variables , You have to resort to + Operators , The specific format is :

strname = str1 + str2

Of course ,+ Operators can also concatenate string constants .

【 Example 】 Use + Operators concatenate strings :

name = "C++ course "
url = "http://c.biancheng.net/cplus/"
info = name + " Our website is :" + url
print(info)

Running results :

C++ The tutorial URL is :http://c.biancheng.net/cplus/

Python String and number splicing

In many application scenarios , We need to put strings and numbers together , and Python Direct splicing of numbers and strings is not allowed , So we have to first convert numbers into strings . Can use str() and repr() Function to convert a number to a string , They are in the form of :

str(obj)
repr(obj)

obj Represents the object to be converted , It can be numbers 、 list 、 Tuples 、 Dictionaries and other types of data .

Look at the code below :

name = "C Chinese language network "
age = 8
course = 30
info = name + " already " + str(age) + " Year old , A total of " + repr(course) + " Set of tutorials ."
print(info)

Running results :

C Chinese language network has 8 Year old , A total of 30 Set of tutorials .

str() and repr() The difference between

str() and repr() Functions can convert numbers to strings , But there is a difference between them :

  • str() Used to convert data into a string form suitable for human reading .
  • repr() Used to convert data into string form suitable for interpreter reading (Python Form of expression ), Suitable for use in the development and debugging phase ; If there is no equivalent grammar , It will happen SyntaxError abnormal .

Please see the following example :

s = "http://c.biancheng.net/shell/"
s_str = str(s)
s_repr = repr(s)
print( type(s_str) )
print (s_str)
print( type(s_repr) )
print (s_repr)

Running results :

<class ‘str’>
http://c.biancheng.net/shell/
<class ‘str’>
‘http://c.biancheng.net/shell/’

In this case ,s It's a string itself , But we still use str() and repr() It's converted . As you can see from the results ,str() It retains the original appearance of the string , and repr() Surround the string with quotation marks , This is it. Python The expression form of string .

in addition , stay Python Enter an expression in an interactive programming environment ( Variable 、 Add, subtract, multiply and divide 、 Logical operations, etc ) when ,Python Will be used automatically repr() Function handles the expression .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved