Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use split in python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to use split in python". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use split in python" can help you solve the problem.

First, function description

1. Split () function

Syntax: str.split (str= "", num=string.count (str)) [n]

Parameter description:

Str: represents as a delimiter, defaults to a space, but cannot be empty (''). If there is no delimiter in the string, the entire string is taken as an element of the list

Num: indicates the number of splits. If there is a parameter num, it is only separated into num+1 substrings, and each substring can be assigned to a new variable

[n]: indicates that the nth slice is selected

Note: when using spaces as delimiters, items that are empty in the middle are automatically ignored

2. Os.path.split () function

Syntax: os.path.split ('PATH')

Parameter description:

PATH refers to the full path of a file as a parameter:

If a directory and file name are given, the output path and file name

If a directory name is given, the output path and empty file name

II. Examples

1. Example 1 shows how to use the split () function.

#! / usr/bin/python#-*-coding: UTF-8-*-str = "Line1-abcdef\ nLine2-abc\ nLine4-abcd"; print str.split (); # is delimited by spaces, including\ nprint str.split (', 1); and # is separated by spaces into two

The output of the above example is as follows:

['Line1-abcdef',' Line2-abc', 'Line4-abcd'] [' Line1-abcdef','\ nLine2-abc\ nLine4-abcd']

2. Example 2, with the # sign as the separator, specify the second parameter as 1, and return two parameter lists.

#! / usr/bin/python#-*-coding: UTF-8-*-txt = "Google#Runoob#Taobao#Facebook" # the second parameter is 1 and returns two parameter lists x = txt.split ("#", 1) print x

The output of the above example is as follows:

['Google',' Runoob#Taobao#Facebook'] this is the end of the introduction to "how to use split in python". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report