Python Forum
Select single letters from string based on separate string - 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: Select single letters from string based on separate string (/thread-23082.html)



Select single letters from string based on separate string - Jpen10 - Dec-10-2019

I've done this in Excel using the =Lookup({}) function, but have no clue how to in Python. String 1 (12 digits): 085236752048. String 2 (10 letters): @ABCDEFGHI. I need to select 5 letters from string 2, based on the values of string 1 from the 7th-11th digit. In this case the result would be; GEB@D. I've tried slice(), but found that I can't index a slice. Thanks for any help. (this is not critical.)


RE: Select single letters from string based on separate string - ibreeden - Dec-10-2019

You are asking complicated things. Are you inventing a new encryption mechanism?
You may solve it in this way:
s1 = '085236752048'
s2 = '@ABCDEFGHI'
print(''.join(s2[int(ch)] for ch in s1[6:11]))
Output:
GEB@D
Explanation:
for ch in s1[6:11]: Walk through the numbers in string 1 (mind you: index starts at 0 and last index is not included)
s2[int(ch)]: make an integer of the character found in the loop and consider this as an index in string 2.
''.join(...): start with an empty string and join this successively with the characters found.


RE: Select single letters from string based on separate string - Jpen10 - Dec-11-2019

I Breeden, Thank you very much. I have a font that is specific to UPCA. the bars are set for consecutive letters and a couple of symbols. The solution you provided gave me the necessary code to equate the two. I can now enter the 11 numbers and it will figure the check Digit, then it converts the entire 12 digits to the font readable text for a UPCA symbol. I added, with your code, the first number, the next 5 numbers, the separator, the last five (your code) and the check Digit. I've worked on this ever since since I coded a program in 1983 in Basic and QBasic. I moved on to Excel and then to Microsoft Access, both with macros in Visual Basic. The program won't produce a scanner readable UPCA symbol. Again my thanks.
Jim


RE: Select single letters from string based on separate string - Jpen10 - Dec-15-2019

Ibreeden, A couple of days ago I finally achieved a scanner readable label with win10,Spyder,Tkinter.