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 split and concatenate Python strings

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to split and connect Python strings. I hope you will get something after reading this article. Let's discuss it together.

Split string

In Python, strings are represented as str objects, and they are immutable: this means that objects represented in memory cannot be changed directly. These two facts can help you learn (and then remember) how to use .split ().

Have you guessed how these two properties of strings relate to the splitting function in Python? If you guess that .split () is an instance method, because the string is a special type, then you are right! In some other languages, such as Perl, the original string is used as input to the stand-alone .split () function rather than as a method called on the string itself.

Note: the method of calling the string method

The string method .split () like this is mainly displayed here as an instance method called on the string. They can also be called static methods, but this is not ideal because they are more "verbose". For completeness, here is an example:

# Avoid this:str.split ('a _

When you compare it to the preferred usage, it is both cumbersome and clumsy:

# Do this instead:'a,b,c'.split (',')

For more information about instances, classes, and static methods in Python, check out our in-depth tutorial.

How about string invariance? This should remind you that string methods are not operated in place, but they return a new object in memory.

Note: operate on the spot

The in-place operation is to directly change the operation of the object that calls them. A common example is the .append () method used on a list: when you call a list, change the list directly by adding input to the same list. .append () .append ()

No-parameter split

Before we go any further, let's look at a simple example:

> 'this is my string'.split () [' this', 'is',' my', 'string']

This is actually a special case of the .split () call, which I chose for its simplicity. No delimiter is specified, and .split () counts any space as a delimiter.

Another feature of naked calls. Split () is that it automatically removes leading and trailing spaces, as well as consecutive spaces. Compare .split () to call the following string without a delimiter argument and a call with''as the delimiter parameter:

> s = 'this is my string' > s.split () [' this', 'is',' my', 'string'] > s.split ('') ['', 'this',' is','', 'my',' string',']

The first thing to note is that this shows the invariance of strings in Python: a subsequent call to .split () processes the original string instead of calling .split () for the first time.

The second and main thing you should see is that bare. Split () call extracts the words in the sentence and discards any spaces.

Specify delimiter

Split (''), on the other hand, is more literal. When there is a leading or trailing delimiter, you get an empty string, which you can see in the first and last elements of the result list.

If there are multiple consecutive delimiters (for example, between "this" and "is" and between "is" and "my"), the first will be used as the delimiter, and the subsequent delimiter will enter your result list as an empty string.

Note: the delimiter .split () in the call

Although the above example uses a single space character as the delimiter input .split (), there is no limit to the character type or string length used as the delimiter. The only requirement is that your delimiter is a string. You can use it from "." Anything to even "separator".

Use Maxsplit to restrict split

Split () has another optional parameter called maxsplit. By default,. Split () will do all possible splits when called. Maxsplit however, when you assign a value to, only a given number of splits are performed. Using our previous example string, we can see that maxsplit:

> s = "this is my string" > > s.split (maxsplit=1) ['this',' is my string']

As shown above, if maxsplit is set to 1, the first white space will be used as the delimiter and the rest will be ignored. Let's do some exercises to test everything we have learned so far.

Exercise: "try it yourself: Maxsplit" shows hidden

What happens when you give a negative number as a maxsplit parameter?

Solution: "try it yourself: Maxsplit" shows hidden

.split () splits your string on all available delimiters, which is the default behavior when maxsplit is not set.

Exercise: partial understanding check shows hiding

You recently received a comma separated values (CSV) file in a very bad format. Your job is to extract each row into a list, and each element of the list represents the columns of the file. What makes it malformed? the address field contains multiple commas, but needs to be represented as a single element in the list!

Suppose your file has been loaded into memory as the following multiline string:

Name,Phone,AddressMike Smith,15554218841123 Nice St, Roy, NM, USAAnita Hernandez,15557789941425 Sunny St, New York, NY, USAGuido van Rossum,315558730,Science Park 123, 1098 XG Amsterdam, NL

Your output should be a list:

['Mike Smith',' 15554218841, '123 Nice St, Roy, NM, USA'], [' Anita Hernandez', '15557789941,' 425 Sunny St, New York, NY, USA'], ['Guido van Rossum',' 315558730, 'Science Park 123,1098 XG Amsterdam, NL']]

Each internal list represents the CSV lines we are interested in, while the external list keeps them together.

Solution: "partial understanding check" shows hidden

This is my solution. There are several ways to attack it. The important thing is that you used .split () for all its optional parameters and got the expected output:

Input_string = "" Name,Phone,AddressMike Smith,15554218841123 Nice St, Roy, NM, USAAnita Hernandez,15557789941425 Sunny St, New York, NY, USAGuido van Rossum,315558730,Science Park 123,1098 XG Amsterdam, NL "" def string_split_ex (unsplit): results = [] # Bonus points for using splitlines () here instead, # which will be more readable for line in unsplit.split ('\ n') [1:]: results.append (line.split (',') Maxsplit=2)) return resultsprint (string_split_ex (input_string))

We. Split () called here twice. It may look scary the first time you use it, but don't worry! We will complete it step by step, and you will be satisfied with these expressions. Let's look at the first .split () call: unsplit.split ('\ n') [1:].

The first element is unsplit, which simply points to the variable of the input string. Then we have our .split () phone: .split ('\ n'). Here, we are splitting a special character called a newline character.

What's the use\ n? As the name implies, it tells anyone who is reading a string that each character after it should be displayed on the next line. In multiline strings like ours, there is a hidden\ n at the end of each line input_string.

The last part may be new: [1:]. The statement so far has given us a new list in memory, [1:] looks like a list index symbol, it is-- a bit! This extended index symbol gives us a list slice. In this case, we take element 1 at index and all elements that follow it, and discard element 0 at index.

In summary, we iterate through a list of strings, where each element represents each line in the multi-line input string except the first line.

In each string, we. Split () again calls using as the split character, but this time we only split it with the first two commas of maxsplit, and the address remains the same. Then we append the result of the call to the properly named results array and return it to the caller.

Connection and connection string

Another basic string operation is the opposite of splitting a string: string concatenation. If you haven't seen the word before, don't worry. It's just a strange way to say "stick together".

Connect with the + operator

There are several ways to do this, depending on what you want to achieve. The simplest and most common method is to use the plus sign (+) to add multiple strings. Just put a between any number of strings you want to concatenate:

>'a'+'b' + 'caterpillar abc'

To be consistent with the mathematical topic, you can also multiply the string by repeating it:

> 'do' * 2'

Remember, strings are immutable! If you concatenate or repeat a string stored in a variable, you must assign the new string to another variable to retain it.

> orig_string = 'Hello' > orig_string +', world''Hello, world' > orig_string'Hello' > full_sentence = orig_string +', world' > full_sentence'Hello, world'

If we don't have an immutable string, full_sentence will output 'Hello, world, world'..

Another consideration is that Python does not do implicit string conversion. If you try to concatenate a string with a non-string type, Python raises a TypeError:

> 'Hello' + 2Traceback (most recent call last): File ", line 1, in TypeError: must be str, not int

This is because you can only concatenate strings with other strings, which may be a new behavior for you if you come from a language like JavaScript that attempts implicit type conversion.

From list to string .join () in Python

There is another, more powerful way to concatenate strings together. You can use this join () method to convert a list in Python to a string.

A common use case here is when you have an iterable object (such as a list) made up of strings and you want to combine these strings into one string. Just like .split (), .join () is a string instance method. If all your strings were in an iterable object, which .join () would you call?

This is a bit of a thorny problem. Remember that when you use the. split (), you will call it on the string or character you want to split. The opposite operation is .join (), so you can call it on the string or character you want to use to concatenate iterable strings together:

> strings = ['do',' re', 'mi'] >', '.join (strings)' do,re,mi'

Here, we strings concatenate each element of the list with a comma (,) and call .join () instead of the strings list.

Exercise: "improve readability by adding" to show hiding

How to make the output text more readable?

Solution: "improve readability by adding" show hidden

One thing you can do is add spacing:

> strings = ['do',' re', 'mi'] >', '.join (strings)' do, re, mi'

By adding a space to our connection string, we greatly improve the readability of the output. You should always keep this in mind when adding strings to improve readability.

Join () is smart because it inserts your "joiner" between the iterable strings you want to add, rather than just adding your joiner at the end of each iterable string. This means that if you pass iteration 1 of size, you will not see your enrollees:

> 'b'.join ([' a'])'a'

Exercise: partial understanding check shows hiding

Using our web crawling tutorial, you have built a great weather crawling tool. However, it loads string information in list lists, each containing unique lines of information to write to the CSV file:

[['Boston',' MA', '76F,' 65% Precip', '0.15 in'], [' San Francisco', 'CA',' 62F,'20% Precip', '0.00 in'], [' Washington', 'DC',' 82F,'80% Precip', '0.19 in'], [' Miami', 'FL',' 79F,'50% Precip'] '0.70 in']]

Your output should be a single string like this:

"" Boston,MA,76F,65% Precip,0.15inSan Francisco,CA,62F,20% Precip,0.00 inWashington,DC,82F,80% Precip,0.19 inMiami,FL,79F,50% Precip,0.70 in "

Solution: "partial understanding check" shows hidden

For this solution, I used the list-driven style, which is a powerful feature of Python that allows you to build lists quickly. If you want to learn more about them, check out this wonderful article that covers all the deductions available in Python.

Here is my solution, starting with a list and ending with a single string:

Input_list = [['Boston',' MA', '76F,' 65% Precip', '0.15 in'], [' San Francisco', 'CA',' 62F,'20% Precip', '0.00 in'], [' Washington', 'DC',' 82F,'80% Precip', '0.19 in'], [' Miami', 'FL',' 79F,'50% Precip'] '0.70 in']] # We start with joining each inner list into a single stringjoined = [', '.join (row) for row in input_list] # Now we transform the list of strings into a single stringoutput ='\ n'.join (joined) print (output)

Here we use .join () not once, but twice. First, we use it in list derivation, which combines all the strings in each internal list into a single string. Next, we concatenate each string with\ nthe newline character we saw earlier. Finally, we simply print the result so that we can verify that it meets our expectations.

After reading this article, I believe you have a certain understanding of "how to split and concatenate Python strings". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

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

Development

Wechat

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

12
Report