String in a list - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: String in a list (/thread-34658.html) |
String in a list - willpe - Aug-18-2021 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. RE: String in a list - ndc85430 - Aug-18-2021 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. RE: String in a list - DeaD_EyE - Aug-18-2021 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. RE: String in a list - deanhystad - Aug-18-2021 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) 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) |