Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - How to Concatenate Strings in a Successive Way?

could you tell me whether there is a more elegant solution to my code what I need?

My little program takes text input from up to seven entry fields (exact number can be specified by the user). The input strings are then concatenated with interspersed fixed text elements. A single output in the form of a string is generated.

The program works perfectly fine. Yet, my programming strikes me as very inelegant and non-pythonic. Could you help me to improve, to foster my learning?

Two points:

1) I like to sum up the string length of all seven text inputs. I plainly added them, which is not elegant. How could I do it a better way?

string_length = len(str(E1.get())) + len(str(E2.get())) + len(str(E3.get())) +   len(str(E4.get())) + len(str(E5.get())) + len(str(E6.get())) + len(str(E7.get()))

2) Depending on the number of entry field which can be specified by the user using a Tkinter Scale S, Entry fields(En) are enabled or disabled for taking input.

The Strings str(En.get()) derived from these Entry field are then concatenated with the fixed string elements partM in between to give rise to the string m. (The whole string is flanked with partL and partR on each side also.) I have done this by query the variable of the scale S (number of entry fields) via the get() method. The program then decides how to proceed via if / elif?

The higher the scale is set the more text elements are added. Yet this is highly similar task which strikes me as there might be a more straight forward solution.

This creates long lines of code and is not very amenable for scaling up the program for taking more Entry fields. How could I do it better?

code excerpt:

if S.get() == 1:
    if string_length == 22*S.get():        
        m = partL + str(E1.get()) + partR
        do_something()
    else:
        warning()

elif S.get() == 2:
    if string_length == 22*S.get():            
        m = partL + str(E1.get()) + partM + str(E2.get()) + partR
        do_something()
    else:
        warning()

elif S.get() == 3:
    if string_length == 22*S.get():
        m = partL + str(E1.get()) + partM + str(E2.get()) + partM + str(E3.get()) + partR
        do_something()
    else:
        warning()

I am very new to Python. I am also new to programming. Yet I am proud I write a little code that works fine and does something useful for me and others. I welcome any suggestions.

like image 281
user3063596 Avatar asked Dec 03 '13 23:12

user3063596


People also ask

How do you concatenate a string multiple times in Python?

Method 1: String Concatenation using + Operator It's very easy to use the + operator for string concatenation. This operator can be used to add multiple strings together.

How do I concatenate a string multiple times?

In Python, you can concatenate two different strings together and also the same string to itself multiple times using + and the * operator respectively.

How do you concatenate a sequence in Python?

You can concatenate sequences of the same type with the + operator. You can multiply a sequence S by an integer n with the * operator. S * n or n * S is the concatenation of n copies of S . When n <=0 , S * n is an empty sequence of the same type as S .

What is the most efficient way to concatenate many strings together Python?

If you are concatenating a list of strings, then the preferred way is to use join() as it accepts a list of strings and concatenates them and is most readable in this case.


2 Answers

If your fields are E1 through E7 and each has a get method this should give you the total length of them:

fields = [E1, E2, E3, E4, E5, E6, E7]
total_length = sum(len(str(f.get())) for f in fields)

If you just want the values collected, this suffices:

fields = [E1, E2, E3, E4, E5, E6, E7]
values = [f.get() for f in fields]
like image 118
g.d.d.c Avatar answered Sep 29 '22 19:09

g.d.d.c


i'll try answer the second part of the question since @g.d.d.c already has a very good answer to the first part.

To avoid code repeat you should look at common sections among your existing code.

if string_length == 22*S.get():
    ...
else:
    warning()

appears in all three cases. Therefore you can extract it to be:

if string_length != 22*S.get():
    warning()
else:     
    if S.get() == 1:
        ...
    elif S.get() == 2:
        ...
    elif S.get() == 3:
        ...
    do_something()

if you foresee to support more cases, you can expand with a function map

functions = {1 : one,
                2 : two,
                3 : three
}

def one():
    m = partL + str(E1.get()) + partR

def two():
    m = partL + str(E1.get()) + partM + str(E2.get()) + partR

def three():
    m = partL + str(E1.get()) + partM + str(E2.get()) + partM + str(E3.get())

and you call with:

functions[S.get()]()
like image 28
WindowsMaker Avatar answered Sep 29 '22 20:09

WindowsMaker