Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String in a list
#1
Hello, I'm new to Python and I have a difficulty and I would like some help.

In a certain situation, I need to get a sequence in a list, but at the time of printing I can not get the sequence as in the example below:

EAN = '0036880008152317,004753b02142802,00138f825183,0246b6513b2f7'
for EAN in EAN:
    print(EAN)
What I would like to print is the first string 0036880008152317

Thank you in advance for your attention.
Reply
#2
You only have one string there, so iterating over it will give you strings of length 1. If you want the items separated by commas, then split the string on the comma and you'll have a sequence of the items.
Reply
#3
If you iterate over a str, you'll get single characters as str.

EAN is a str. I guess you want to split the str at the commas.

EAN = '0036880008152317,004753b02142802,00138f825183,0246b6513b2f7'
for EAN in EAN.split(","):
    print(EAN)
You should look what str.split does.
willpe likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
I think you copied something wrong. Since you are expecting a list of strings should the code be this?
EAN = '0036880008152317', '004753b02142802', '00138f825183', '0246b6513b2f7'
for EAN in EAN:
    print(EAN)
Output:
0036880008152317 004753b02142802 00138f825183 0246b6513b2f7
I don't like "for EAN in EAN:". The original list EAN is now gone forever. If EAN is so unimportant why not write as:
for EAN in ['0036880008152317', '004753b02142802', '00138f825183', '0246b6513b2f7']:
    print(EAN)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I converted string to 'list', but it doesn't look like a list! mrapple2020 3 3,246 Apr-07-2019, 02:34 PM
Last Post: mrapple2020
  Create Alert if string from list appears on other list javalava 1 2,517 Sep-17-2018, 02:44 PM
Last Post: DeaD_EyE
  List of pathlib.Paths Not Ordered As Same List of Same String Filenames QbLearningPython 20 15,404 Nov-16-2017, 04:47 PM
Last Post: QbLearningPython
  Create a new list by comparing values in a list and string DBS 2 3,535 Jan-14-2017, 07:59 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020