0% found this document useful (0 votes)
2 views1 page

Grouping Strings in Python Lists

The document discusses methods for grouping strings in Python lists. It explains how to break a string into individual characters using list() and provides correct methods for grouping characters, including manual slicing, regular expressions, and using split() for delimited strings. Key techniques include slicing the string and utilizing regex for flexible grouping.

Uploaded by

agarwalharsh7373
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Grouping Strings in Python Lists

The document discusses methods for grouping strings in Python lists. It explains how to break a string into individual characters using list() and provides correct methods for grouping characters, including manual slicing, regular expressions, and using split() for delimited strings. Key techniques include slicing the string and utilizing regex for flexible grouping.

Uploaded by

agarwalharsh7373
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python List: Grouping Strings & Modifications

Working with Strings in Lists

Using list("abc"):

- list("abc") ['a', 'b', 'c']

- Breaks string into individual characters.

Correct Methods to Get ['ab', 'c']:

1. Manual Slicing:

s = "abc"

my_list = [s[:2], s[2:]] # ['ab', 'c']

2. Using Regular Expressions:

import re

s = "abc"

my_list = [Link](r'.{1,2}', s) # ['ab', 'c']

3. Using Split (if string is delimited):

s = "ab,c"

my_list = [Link](',') # ['ab', 'c']

Summary:

- list("abc") splits into single characters.

- To group characters, use slicing or regex.

- If the input has a delimiter, use split().

You might also like