Python Forum
Concatinating two strings to make a variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Concatinating two strings to make a variable
#6
(Jun-20-2017, 06:08 PM)DeaD_EyE Wrote:
(Jun-20-2017, 09:49 AM)bencouve Wrote: Perfect! Thanks ...

and ok

first post

Please don't do this. You're using the wrong data structure. Maybe you want to have a dict:

data = {'i0': 'This', 'i1': 'is', 'i2': 'the', 'i3': 'string'}

# direct access
print(data['i0'])

# iterating over values
# unordered

for value in data.values():
   print(value)

# sorted access
# sorted sorts by first element, if the the yielded element is a list/tuple
for key, value in sorted(data.items()):
   print(value)

# or preserve the order:
from collections import OrderedDict
data = [('i0', 'This'), ('i1', 'is'), ('i2', 'the'), ('i3', 'string')]
ordered_dict = OrderedDict(data)

for value in ordered_dict.values():
   print(value)
Since Python 3.6 dicts preserve the order, but we should not rely on it. Maybe with next version they guarantee it.
Currently it's just an implementation detail.

... or even simpler, an array:

i = [ 'This','is','the', 'string' ]  # Stylistically `i` is a bad name for an array

# direct access
print(i[0])

# iterating over value (always ordered)
for value in i:
   print(value)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Messages In This Thread
RE: Concatinating two strings to make a variable - by Ofnuts - Jun-21-2017, 10:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 811 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,814 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  extract particular strings into a variable lokesh 3 2,141 Dec-09-2020, 11:59 AM
Last Post: lokesh
  Finding multiple strings between the two same strings Slither 1 2,543 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  How to I define a variable between strings in telnetlib write? Fez 2 3,419 May-02-2019, 06:53 PM
Last Post: Fez
  How do you make functions that take a variable that is not defined? magic 6 4,480 Sep-24-2018, 01:30 PM
Last Post: gruntfutuk
  Trying to make two different strings from the same root De1uge 0 2,045 Jun-09-2018, 04:06 PM
Last Post: De1uge
  Spyder IDE - make variable explorer to follow the color scheme of the Editor Antonio 0 5,470 May-05-2018, 10:20 PM
Last Post: Antonio
  lists, strings, and byte strings Skaperen 2 4,259 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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