⊗pyPmLsSSL 40 of 208 menu

Splitting a String into a List in Python

We can also create a list from a string. To do this, we must split it using the split method. In the method parameter, we specify the separator by which we will split the string. Just keep in mind that this separator must also be in the string itself:

txt = '1-2-3-4' print(txt.split('-')) # ['1', '2', '3', '4']

If you don't pass a separator to the split method, it will simply convert the entire string into a single list element:

txt = '1-2-3-4' print(txt.split()) # ['1-2-3-4']

The following code is given:

txt = 'a,b,c,d,e' print(txt.split(','))

Tell me what will be output to the console.

The following code is given:

txt = 'a_bc_de' print(txt.split('_'))

Tell me what will be output to the console.

The following code is given:

txt = 'ab 12 cd' print(txt.split(''))

Tell me what will be output to the console.

The following code is given:

txt = '1 23 45' print(txt.split(' '))

Tell me what will be output to the console.

The following code is given:

txt = '123_45' print(txt.split())

Tell me what will be output to the console.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline