python脚本字符串拼接
This article gives an overview of Python Script functions to split strings and string concatenation functions.
本文概述了用于拆分字符串和字符串连接函数的Python脚本函数。
Python is a versatile language. It contains many useful functions, libraries, modules that provide you with the flexibility to write your code. SQL Server 2017 onwards we can execute Python codes inside SQL Server. You need to install Machine learning services for using Python in SQL Server.
Python是一种通用语言。 它包含许多有用的功能,库和模块,可为您提供编写代码的灵活性。 从SQL Server 2017开始,我们可以在SQL Server中执行Python代码。 您需要安装机器学习服务才能在SQL Server中使用Python。
I would recommend you to go through the following lines before proceeding with this article.
我建议您在继续本文之前先完成以下几行。
In this article, we explore Python useful function to SPLIT and do string concatenation using Python Scripts.
在本文中,我们探索了Python对SPLIT有用的函数,并使用Python脚本进行了字符串连接。
In Python, we do not have a character data type. It uses Unicode characters for the string. It also considers a single character as a string. Sometimes, we need to split a string based on the separator defined. It is similar to a text to columns feature in Microsoft Excel.
在Python中,我们没有字符数据类型。 它使用Unicode字符作为字符串。 它还将单个字符视为字符串。 有时,我们需要根据定义的分隔符分割字符串。 它类似于Microsoft Excel中的“文本到列”功能。
Look at the following code. Here, we use the SPLIT function without any separator.
看下面的代码。 在这里,我们使用不带分隔符的SPLIT函数。
a = 'You are exploring Python script function SPLIT'
print(a.split())
It breaks the string into smaller chunks. By default, it considers space as a string separator. In the above query, we get split strings on each occurrence of white space.
它将字符串分成较小的块。 默认情况下,它将空格视为字符串分隔符。 在上面的查询中,我们在每次出现空白时都会得到分割字符串。
Now, we make a slight change in the Python Script. It contains special characters (comma and separator ).
现在,我们对Python脚本进行了一些更改。 它包含特殊字符(逗号和分隔符)。
a = 'Hi, You are exploring Python script function - SPLIT'
print(a.split())
If we remove space from between two words (for example Hi, You in below example), it does not split them.
如果我们从两个单词之间删除空格(例如,下面的示例中的“嗨,您”),它不会将它们分开。
a = 'Hi,You are exploring Python script function - SPLIT'
print(a.split())
Here, in the output, we can note the difference.
在这里,在输出中,我们可以注意到差异。
We can use the following parameters in the SPLIT function.
我们可以在SPLIT函数中使用以下参数。
Separator: In the Excel’s text to column functionality, we define a separator such as a comma, semicolon to split a string. Similarly, we use a separator in Python to split it on the occurrence of separator
分隔符:在Excel的文本到列功能中,我们定义了一个分隔符(例如逗号,分号)来分割字符串。 同样,我们在Python中使用分隔符在出现分隔符时对其进行拆分
In the following script, we use a comma separator. Once we execute this script, it splits the string on the occurrence of a comma separator.
在以下脚本中,我们使用逗号分隔符。 一旦执行此脚本,它将在出现逗号分隔符时拆分字符串。
a = 'Hi,You are exploring Python script function - SPLIT'
print(a.split(","))
In the case of multiple separators, the string gets splits on each occurrence of the separator, as shown below:
对于多个分隔符,字符串在每次出现分隔符时都会被拆分,如下所示:
a = 'Hi,You are exploring Python script function, - SPLIT'
print(a.split(","))
Output:
输出:
Max Number of splits
最大分割数
We can specify a maximum number of splits in a string. It is an optional parameter. By default, Python function split the complete string based on the separator defined. It splits on each occurrence of the separator
我们可以在字符串中指定最大分割数。 它是一个可选参数。 默认情况下,Python函数根据定义的分隔符拆分完整的字符串。 每次出现分隔符时都会拆分
In the below script, we have multiple separators. Suppose we need a certain number of string splits using Python scripts. In this case. We use maximum splits of the string parameter. Therefore, we specify value 4 in the SPLIT function to tell that it should stop splitting string after 4 splits
在下面的脚本中,我们有多个分隔符。 假设我们需要使用Python脚本进行一定数量的字符串拆分。 在这种情况下。 我们使用string参数的最大分割数。 因此,我们在SPLIT函数中指定值4告诉它应该在4个分割后停止分割字符串
a = 'Hi-You-are-exploring-Python-script-function-SPLIT'
print(a.split("-",4))
We can verify splits in the following output:
我们可以在以下输出中验证拆分:
We can use a string as a delimiter as well in Python Scripts. In the following code, we want to split a string based on ‘And’ separator
我们也可以在Python脚本中使用字符串作为定界符。 在下面的代码中,我们想基于'And'分隔符分割字符串
Names= 'Raj and Kusum and Akshita and Guddu and Family'
# maxsplit of 1
print(Names.split(' and ',1))
# maxsplit of 2
print(Names.split(' and ',2))
# maxsplit of 3
print(Names.split(' and ',3))
The output shows a split string based on string delimiter and a maximum number of split values
输出显示基于字符串定界符的拆分字符串和最大拆分值数量
In the previous section, we split the string based on a delimiter and a maximum number of split parameters. Sometimes we also require to join the strings together. For example, suppose we have two datasets containing first and last name of the customer. We want to concatenate string together.
在上一节中,我们基于分隔符和最大数量的拆分参数拆分字符串。 有时我们还需要将字符串连接在一起。 例如,假设我们有两个包含客户名字和姓氏的数据集。 我们想将字符串连接在一起。
We have several ways to concatenate strings in Python scripts. Let’s look at few such ways.
我们有几种方法可以在Python脚本中串联字符串。 让我们看看几种这样的方式。
It is a common way of concatenate in most languages. We use a plus operator to concentrate two or more strings using this way. We use the plus operator in SQL Server as well however we have many functions available for it as well.
这是大多数语言中连接的一种常见方式。 我们使用加号运算符以这种方式集中两个或多个字符串。 我们也在SQL Server中使用加号运算符,但是我们也有许多可用的功能。
P1 = 'Apple'
P2 = 'Banana'
P3 = 'Orange'
P4 = P1+ P2 + P3
print(P4)
In the above example, we concatenate three strings together using the plus operator and the output is a concatenated string as shown below.
在上面的示例中,我们使用加号运算符将三个字符串连接在一起,并且输出是一个连接字符串,如下所示。
We can add white space or any special characters in the string using a similar plus operator. For example, in the below code, we specify space and separator in a string.
我们可以使用类似的加号运算符在字符串中添加空格或任何特殊字符。 例如,在下面的代码中,我们在字符串中指定空格和分隔符。
P1 = 'Apple'
P2 = ' - '
P3 = 'Orange'
P4 = P1+ P2 + P3
print(P4)
Here, we get the output with a white space between the first and second words:
在这里,我们得到的输出在第一个词和第二个词之间有一个空格:
Python uses a JOIN function to concatenate multiple strings together. In this function, we can specify separator as well.
Python使用JOIN函数将多个字符串连接在一起。 在此函数中,我们还可以指定分隔符。
In this example, instead of specifying the strings directly, we ask for user input for entering a string. In the print statement, we specify white space in the double quote and use the JOIN function to concatenate strings.
在此示例中,我们要求用户输入输入字符串,而不是直接指定字符串。 在print语句中,我们在双引号中指定空格,然后使用JOIN函数连接字符串。
String1 = input('Please enter the first string:\n')
String2 = input('Please enter the second string:\n')
print('The Concatenated String using join() function=', " ".join([String1, String2]))
Once you execute this code, it asks for the first user input.
一旦执行了此代码,它将要求输入第一位用户。
Write the string in the box and press Enter. It asks for the second string.
在框中输入字符串,然后按Enter。 它要求第二个字符串。
Press Enter, and it returns the concatenated string.
按Enter键,它返回连接的字符串。
We can also use FORMAT function in Python scripts to perform string concatenations. We can also use separators or delimiters in this function.
我们还可以在Python脚本中使用FORMAT函数来执行字符串连接。 我们也可以在此函数中使用分隔符或定界符。
In the following query, we use white space between two Brackets and specify strings in the format function.
在以下查询中,我们使用两个方括号之间的空格,并在format函数中指定字符串。
string1 = 'Rajendra'
string2 = 'Gupta'
Output = "{} {}".format(string1, string2)
print(Output)
In the output, we get a white space between as a result of string concatenation. Let’s specify a special character in the format function between the brackets.
在输出中,由于字符串连接,我们之间有一个空格。 让我们在方括号之间的格式函数中指定一个特殊字符。
string1 = 'This pen cost is'
string2 = '5$'
Output = "{} - {}".format(string1, string2)
print(Output)
In the output, you get the character between the strings.
在输出中,您将获得字符串之间的字符。
We can use f strings ( in Python 3.6+) for string concatenation as well. It is also known as Literal String Interpolation. In the F-strings, we use the embed expressions inside string literals. We use a prefix f in the code.
我们也可以使用f字符串(在Python 3.6+中)进行字符串连接。 也称为文字字符串插值。 在F字符串中,我们在字符串文字中使用embed表达式。 我们在代码中使用前缀f。
In the following code, we specify string concatenate using the f strings.
在下面的代码中,我们使用f字符串指定字符串连接。
string1 = 'Rajendra'
string2 = 'Gupta'
Output = f'{string1} {string2}'
print('String Concatenation using f-string =', Output)
We get the following concatenated string output using f strings function.
我们使用f strings函数获得以下串联的字符串输出。
In this article, we explored Python functions to split and concatenate strings in a Python script. You should be familiar with the available function to use it appropriately. You can use them in Python SQL scripts as well to run Python code from SQL Server. We will cover more useful functions and methods in upcoming articles.
在本文中,我们探讨了Python函数以在Python脚本中拆分和连接字符串。 您应该熟悉可用的功能以适当地使用它。 您也可以在Python SQL脚本中使用它们,以从SQL Server运行Python代码。 我们将在后续文章中介绍更多有用的功能和方法。
翻译自: https://www.sqlshack.com/python-scripts-to-split-and-concatenate-strings/
python脚本字符串拼接