How to join every input without every letter being separated in Python?
I´m making a vote system, and i´m making a input, where´s the "user" put the name of the candidate and the name is registrered, but when the "user" ( a.k.a me ) press "0" to exit, they show the list of candidates normally, but the letters show separated and with the comma, like "T, h, o, m, a, s" or "P, e, t, e, r".
I´ve tried making it string, and using the "join" function, but it aplicates the ", " for space in every letter.
def urna(): candidatos =  completo = False while completo == False: adicionar = input("Coloque seus candidatos aqui, ( 0 para sair ): ") if adicionar == "0": candidatos_formatados = " ".join(candidatos) print() print("Fim!") print() print("Esse foi(ram) o(s) candidato(s) registrado(s):", candidatos_formatados) break else: print() candidatos.extend(adicionar) print("Foi adicionado(a) o(a) candidato(a) %s" % adicionar) print()
I expect the output like: "Thomas, Peter, Johann, Hawking, Stephen" and so on; But the actual output of this print is: "T, h, o, m, a, s", look at the image below:
You need to
k =  k.append("Tom") # k == ["Tom"] k.extend("Apple") # K == ["Tom", "A", "p", "p", "l", "e"]
See Difference between append vs. extend list methods in Python and more on lists
Python 3 Notes: Split and Join, Even though it is a sentence, the words are not represented as discreet units. others. .split() separates on any combined sequence of those characters: > Called on a "separator" string 'x', 'x'.join(y) joins every element in the list y separated� Called on a "separator" string 'x', 'x'.join (y) joins every element in the list y separated by 'x'. Below, words in mwords are joined back into the sentence string with a space in between: >>> mwords ['Mary', 'had', 'a', 'little', 'lamb'] >>> ' '.join (mwords) 'Mary had a little lamb' Joining can be done on any separator string.
You should use list.append() function to add adicionar (string) to candidatos (list) instead of list.extend().
See this answer for difference between append() and extend()
Splitting, Concatenating, and Joining Strings in Python – Real Python, Without any separator specified, .split() will count any whitespace as a separator. input to .split() , you aren't limited in the types of characters or length of strings you use You were recently handed a comma-separated value (CSV) file that was The opposite operation is .join() , so you call it on the string or character you� pile = ' '.join(source) This takes the elements of "source" and joins them with a single space as the connector. If you need only the letters separated, then build the list of letters only, and join that: pile = ' '.join([c for c in source if c.isalpha()])
extend(): Iterates over the arguments and add each element to the list.
l = ['hello'] l.extend('world') l = ['hello', 'w', 'o', 'r', 'l', 'd']
append(): Adds its argument as a single element to the end of a list.
l = ['hello'] l.append('world') l = ['hello', 'world']
You need to use
Python program to split and join a string, Another application is CSV(Comma Separated Files). Split the string into list of strings Input : Geeks for Geeks Output : ['Geeks', 'for', 'Geeks'] Join the Below is Python code to Split and Join the string based on a delimiter : Your browser does not currently recognize any of the video formats available. Python Version Note: Should you find yourself working with Python 2.x code, you might bump into a slight difference in the input functions between Python versions 2 and 3. raw_input() in Python 2 reads input from the keyboard and returns it. raw_input() in Python 2 behaves just like input() in Python 3, as described above.
Python program to input a comma separated string, If the separator is not provided then any white space is used as a separator. Generally, users use a split() method to split a Python string but� The join() method provides a flexible way to create strings from iterable objects. It joins each element of an iterable (such as list, string, and tuple) by a string separator (the string on which the join() method is called) and returns the concatenated string. The syntax of the join() method is: string.join(iterable)
Python: Prints the unique words in sorted form from a comma , Python Exercises, Practice and Solution: Write a Python program that accepts [ word for word in items.split(",")] print(",".join(sorted(list(set(words))))). Sample Output: Input comma separated sequence of words red, black, pink, green The following tool visualize what the computer is doing step-by-step as� Method 1: split string into characters python using UDF. Here I am creating the function named split() which should take one argument which will be input. Majorly the string which we are looking to split. This has created one user-defined function in Python with the name split() which take one argument as input which is a string that we want to
The simplest and most common method is to use the plus symbol ( +) to add multiple strings together. Simply place a + between as many strings as you want to join together: >>>. >>> 'a' + 'b' + 'c' 'abc'. In keeping with the math theme, you can also multiply a string to repeat it: >>>. >>> 'do' * 2 'dodo'.