I want to include the reference character when sliding the Python string.

Asked 2 years ago, Updated 2 years ago, 128 views

If you just slice based on certain characters, those characters don't appear, but only the words are cut off and stored in the list. For example "Hello, world." If you slice it according to "," ["Hello", "world"] It goes like this ["Hello", ", "World"] I want to make the standard text come out on the list. Is there any way?

python slice string

2022-09-20 19:38

2 Answers

Please refer to the following

>>> s = "Hello, world"
>>> s.partition(",")
('Hello', ',', ' world')


2022-09-20 19:38

scala> val s = "Hello, world"
s: String = Hello, world

scala> s.split("(?=,)")
res5: Array[String] = Array(Hello, , world)


2022-09-20 19:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.