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.

我建议您在继续本文之前先完成以下几行。

  • Why would a SQL Server DBA be interested in Python? 为什么SQL Server DBA对Python感兴趣?
  • Python articles published on SQLShack 在SQLShack上发布的Python文章

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数据工作室中SQL笔记本 :我使用本文中的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数据工作室 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笔记本中的管理并进行验证
SQL Notebooks in the Azure Data Studio

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 Script function

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),分隔符后有空格,因此它将其视为单独的字符串并将其分割
String function output

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.

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

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

    分隔符:在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:

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

    Max Number of splits

    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

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

    Multiple string delimiters

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.

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

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:

在这里,我们得到的输出在第一个词和第二个词之间有一个空格:

使用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.

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

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键,它返回连接的字符串。

Concatenated string

使用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.

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

Use FORMAT function for string concatenation

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

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

python脚本字符串拼接_Python脚本分割和连接字符串相关推荐

  1. c++ 字符串拼接_python字符串零碎总结

    本文讲一讲与python字符串打交道时的一些注意事项,最初是参考python cookbook一书,后来更新了一部分内容.目录如下 尽量使用字符串方法而不是re库 正则表达式中实现"或&qu ...

  2. python 字符串拼接_Python字符串拼接的6种方法(转)

    add by zhj: 对于多行字符串连接,第6种连接方法很方便,连接时不会添加额外的空格. 1. 加号 第一种,有编程经验的人,估计都知道很多语言里面是用加号连接两个字符串,Python里面也是如此 ...

  3. python里的拼接_Python拼接字符串的7种方法总结

    前言 忘了在哪看到一位编程大牛调侃,他说程序员每天就做两件事,其中之一就是处理字符串.相信不少同学会有同感. 在Python中,我们经常会遇到字符串的拼接问题,几乎任何一种编程语言,都把字符串列为最基 ...

  4. python刷网易云_Python脚本用于定时关闭网易云音乐PC客户端

    本文主要讲述如何使用Python在指定的秒数后关闭Windows上运行的程序(此程序以网易云音乐为例).本文的背景是昨晚发现网易云音乐的PC客户端没有定时关闭的功能,可以使用Python编写一个简单的 ...

  5. python 字符串拼接_Python中拼接字符串的方法 | 萧小寒

    摘要 在编程语言中,几乎每种语言都有关于字符串的操作方法或函数.原因很简单,字符串做为编程语言中不可或缺的数据类型,有着不可以替代的重要性.不同的是,每种编程语言对于字符串的处理方式都有自己的特色.这 ...

  6. python引流脚本开发工具_Python脚本 抖X自动关注粉丝引流脚本

    #python 3.6.4 # encoding:utf-8 #确保已连接好adb #1080 2280 分辨率,一加6测试通过 #抖音版本20200618 import os import cv2 ...

  7. c++字符串拼接_Python零基础入门(三):字符串进阶

    点击蓝字 关注浅韵 一起划水 封面图: 这幅漫画告诉我们:程序不仅仅是写出来就完事的,还要看看能不能实现自己的目的,以及实现目的的方式是不是自己所希望的. 零.目标 1.字符串的增删改查2.字符串和内 ...

  8. python rfind函数用法_Python语法速查:字符串格式简单处理、子串查找与判断方法?...

    这是一篇python基础知识分享型文章,对学习python感兴趣的朋友们可以仔细看看 字符串常用方法 Python3中,字符串全都用Unicode形式,所以省去了很多以前各种转换与声明的麻烦.字符串属 ...

  9. python练习题百度云_Python专项基础练习(字符串)练习题

    1. 字符串练习题 1.1.字符串内置方法练习 在交互式解释器中完成下列题目将字符串 "abcd" 转成大写 计算字符串 "cd" 在 字符串 "ab ...

最新文章

  1. 验证和训练loss和acc多种情况分析
  2. IDEA2021.3无法创建测试类解决方法
  3. Deep Learning(深度学习)学习笔记整理系列之LeNet-5卷积参数个人理解
  4. linux中sudo如何读取标准输入作为密码,避免每次都输入密码?
  5. JavaScript——XMLHttpResquest的简单封装
  6. MySQL 中and 与or的优先级
  7. 华为研发雄起加拿大!
  8. 【离散数学笔记】图的基本概念思维导图
  9. Python基础学习2--字符串
  10. iOS 对象数组生成jsonarray
  11. 图文演示通过虚拟打印机生成pdf的使用技巧
  12. labuladong算法小结
  13. ubuntu 中文版 man
  14. [洛谷1849] 拖拉机
  15. 数据分析之数理统计基础
  16. 163邮箱导出eml格式文件
  17. python控制浏览器脚本_Chrome 33+浏览器 Cookies encrypted_value解密脚本(python实现)...
  18. Windows留后门--教程(一)——Windows系统隐藏账户
  19. otg usb 定位_教你简单认识OTG与OTG线
  20. 计算并输出正整数n的各位数字之积

热门文章

  1. oracle 实时负载查询,Oracle并行查询
  2. 定义一个没有参数的函数、输出python3次_Python函数参数详解,三天让你掌握python,不再是小白
  3. cocos2dx-lua 笔记 方向控制 v2
  4. C#取真实IP地址及分析
  5. 重新打包system.img
  6. ★LeetCode(704)——二分查找(JavaScript)
  7. HTTPS协议的简述
  8. by mybatis 自定义order_springboot2结合mybatis拦截器实现主键自动生成
  9. RFID 打印机是什么
  10. 养老金上涨后,退休老人每个月6500元的养老金,属于什么水平?