Python Forum
Select single letters from string based on separate string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Select single letters from string based on separate string
#1
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.)
Reply
#2
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.
Reply
#3
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
Reply
#4
Ibreeden, A couple of days ago I finally achieved a scanner readable label with win10,Spyder,Tkinter.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] [Beautiful Soup] Replace tag.string from another file? Winfried 2 314 May-01-2025, 03:43 PM
Last Post: Winfried
  Get the string after a specific char JanJan 5 393 Apr-30-2025, 05:04 AM
Last Post: snl_9527
  TypeError: string indices must be integers deneme2 2 693 Feb-14-2025, 12:23 AM
Last Post: deneme2
  How do I parse the string? anna17 8 2,191 Feb-13-2025, 07:08 AM
Last Post: michaeljordan
  question about changing the string value of a list element jacksfrustration 4 2,179 Feb-08-2025, 07:43 AM
Last Post: jacksfrustration
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 1,215 Dec-19-2024, 11:57 AM
Last Post: snippsat
  [SOLVED] Sub string not found in string ? jehoshua 4 1,457 Dec-03-2024, 09:17 PM
Last Post: jehoshua
  extracting from a string Stephanos 6 1,237 Oct-01-2024, 06:52 AM
Last Post: DeaD_EyE
  Unable to understand the function string.split() Hudjefa 8 2,641 Sep-16-2024, 04:25 AM
Last Post: Pedroski55
Question [SOLVED] How to replace characters in a string? Winfried 2 1,084 Sep-04-2024, 01:41 PM
Last Post: Winfried

Forum Jump:

User Panel Messages

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