欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python脚本字符串拼接_Python脚本分割和连接字符串

程序员文章站 2022-07-07 14:08:35
...

python脚本字符串拼接

This article gives an overview of Python Script functions to split strings and string concatenation functions.

本文概述了用于拆分字符串和字符串连接函数的Python脚本函数。

介绍 (Introduction)

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脚本进行了字符串连接。

先决条件 (Prerequisites)

  • SQL Notebooks in the Azure Data Studio: I use the Azure Data Studio march release in this article to create a SQL Notebook and execute code. We can run SQL, Python, Spark, PowerShell codes in the notebook. You can use separate tools as well for it, but I would recommend using it for both T-SQL and Python

    Azure数据工作室中SQL笔记本 :我使用本文中的Azure数据工作室 3月发行版来创建SQL笔记本并执行代码。 我们可以在笔记本中运行SQL,Python,Spark,PowerShell代码。 您也可以使用单独的工具,但是我建议同时将其用于T-SQL和Python
  • PIP Python module: Validate the PIP module in Python. Click on Manage in the SQL notebook of Azure Data Studio and verify it

    PIP Python模块 :验证Python中的PIP模块。 单击Azure Data StudioSQL笔记本中的管理并进行验证

python脚本字符串拼接_Python脚本分割和连接字符串

Python脚本功能– SPLIT (Python Script function – SPLIT)

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中的“文本到列”功能。

不带任何参数的SPLIT函数 (SPLIT function without any arguments)

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.

它将字符串分成较小的块。 默认情况下,它将空格视为字符串分隔符。 在上面的查询中,我们在每次出现空白时都会得到分割字符串。

python脚本字符串拼接_Python脚本分割和连接字符串

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())
  • For string (Hi,), it considers it a single word because it does not contain white space. It does not split it

    对于字符串(Hi,),它将其视为一个单词,因为它不包含空格。 它不会分裂
  • For string (- SPLIT), we have space after separator, so It considers it as a separate string and splits it

    对于字符串(-SPLIT),分隔符后有空格,因此它将其视为单独的字符串并将其分割

python脚本字符串拼接_Python脚本分割和连接字符串

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.

在这里,在输出中,我们可以注意到差异。

python脚本字符串拼接_Python脚本分割和连接字符串

SPLIT函数参数 (SPLIT function arguments)

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

    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:

    Output:

    python脚本字符串拼接_Python脚本分割和连接字符串

    分隔符:在Excel的文本到列功能中,我们定义了一个分隔符(例如逗号,分号)来分割字符串。 同样,我们在Python中使用分隔符在出现分隔符时对其进行拆分

    在以下脚本中,我们使用逗号分隔符。 一旦执行此脚本,它将在出现逗号分隔符时拆分字符串。

    对于多个分隔符,字符串在每次出现分隔符时都会被拆分,如下所示:

    a = 'Hi,You are exploring Python script function, - SPLIT'
     print(a.split(","))

    输出:

  • 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

    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

    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

    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

    最大分割数

    我们可以在字符串中指定最大分割数。 它是一个可选参数。 默认情况下,Python函数根据定义的分隔符拆分完整的字符串。 每次出现分隔符时都会拆分

    在下面的脚本中,我们有多个分隔符。 假设我们需要使用Python脚本进行一定数量的字符串拆分。 在这种情况下。 我们使用string参数的最大分割数。 因此,我们在SPLIT函数中指定值4告诉它应该在4个分割后停止分割字符串

    a = 'Hi-You-are-exploring-Python-script-function-SPLIT'
     print(a.split("-",4))

    我们可以在以下输出中验证拆分:

    我们也可以在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))

    输出显示基于字符串定界符的拆分字符串和最大拆分值数量

Python字符串串联 (Python String Concatenation)

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脚本中串联字符串。 让我们看看几种这样的方式。

使用plus(+)运算符连接: (Concatenate using the plus(+) operator:)

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.

在上面的示例中,我们使用加号运算符将三个字符串连接在一起,并且输出是一个连接字符串,如下所示。

python脚本字符串拼接_Python脚本分割和连接字符串

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脚本字符串拼接_Python脚本分割和连接字符串

使用JOIN函数进行字符串连接 (Use JOIN function for string concatenation)

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.

一旦执行了此代码,它将要求输入第一位用户。

python脚本字符串拼接_Python脚本分割和连接字符串

Write the string in the box and press Enter. It asks for the second string.

在框中输入字符串,然后按Enter。 它要求第二个字符串。

python脚本字符串拼接_Python脚本分割和连接字符串

Press Enter, and it returns the concatenated string.

按Enter键,它返回连接的字符串。

python脚本字符串拼接_Python脚本分割和连接字符串

使用FORMAT函数进行字符串连接 (Use FORMAT function for string concatenation)

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.

在输出中,您将获得字符串之间的字符。

python脚本字符串拼接_Python脚本分割和连接字符串

使用f字符串进行字符串连接 (Use f strings for string concatenation)

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函数获得以下串联的字符串输出。

python脚本字符串拼接_Python脚本分割和连接字符串

结论 (Conclusion)

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脚本字符串拼接