How can I format this list into one string? - python-3.x

I have a list that contains a word. Each letter is separated by a space (as seen below).
word = ["h", " ", "e", " ", "l", " ", "l", " ", "o", " "]
I am trying to get it to print in the format:
h e l l o
I tried using a print statement (among other things) but it just came out:
["h", " ", "e", " ", "l", " ", "l", " ", "o", " "]
How do I fix this?

You could str.join(iterable) to join them together as one string:
"".join(word)
This will join all elements of the array with empty strings, essentially concatenating the strings together into one. Then you can print it:
print("".join(word))
This will produce
h e l l o

Just use the join function to convert the List into a string:
print ("".join(my_word))
The "" before .join means that between the characters an empty space will be added. If you want you can put whatever you like, even spaces or digits or strings.

Related

python printing dictionaries in different order

Below is my code
qas = [
["Apple","Red"],
["Banana", "Yellow"],
["Berries","Blue"]
]
answer_choices = {
"a" : "Red",
"b" : "Yellow",
"c" : "Blue"
}
Answers_count = 0
for qa in qas :
print("Choose from")
for k,v in answer_choices.items() :
print("("+k+") "+" "+v)
ans=input("\nWhat color is "+ qa[0]+" : ")
if answer_choices.get(ans) == qa[1] :
print("Correct \n")
Answers_count += 1
else :
print("Wrong \n")
print("You got "+str(Answers_count)+" correct")
Answers are expected to be printed as (a), (b), (c)
Some how order in which answer choice key value pairs printing is changing with the order of keys in qas.
Please suggest
Python dictionary are not keeping the insertion order of the elements, it's not having a lexicographic ordering as well.
You can try using an OrderedDict in order to keep the insertion order (not lexicographic one).
Another suggestion will be to replace the answer_choices with a list of tuples where the first argument will be the answer key while the seconds one will be the choose.
answer_choices = [("a", "Red"), ("b", "Yellow"), ("c", "Blue")]
for k,v in answer_choices:
print("(" + k + ") " + " " + v)

Julia: concat strings with separator (equivalent of R's paste)

I have an array of strings that I would like to concatenate together with a specific separator.
x = ["A", "B", "C"]
Expected results (with sep = ;):
"A; B; C"
The R's equivalent would be paste(x, sep=";")
I've tried things like string(x) but the result is not what I look for...
Use join. It is not clear if you want ";" or "; " as a separator.
julia> x = ["A", "B", "C"]
3-element Array{String,1}:
"A"
"B"
"C"
julia> join(x, ';')
"A;B;C"
julia> join(x, "; ")
"A; B; C"
If you just want ; then just use a character ';'as a separator, if you also want the space, you need to use a string: "; "

Find and print vowels from a string using a while loop

Study assignment (using python 3):
For a study assignment I need to write a program that prints the indices of all vowels in a string, preferably using a 'while-loop'.
So far I have managed to design a 'for-loop' to get the job done, but I could surely need some help on the 'while-loop'
for-loop solution:
string = input( "Typ in a string: " )
vowels = "a", "e", "i", "o", "u"
indices = ""
for i in string:
if i in vowels:
indices += i
print( indices )
while-loop solution:
string = input( "Typ in a string: " )
vowels = "a", "e", "i", "o", "u"
indices = ""
while i < len( string ):
<code>
i += 1
print( indices )
Would the use 'index()' or 'find()' work here?
Try This :
string = input( "Typ in a string: " )
vowels = ["a", "e", "i", "o", "u"]
higher_bound=1
lower_bound=0
while lower_bound<higher_bound:
convert_str=list(string)
find_vowel=list(set(vowels).intersection(convert_str))
print("Vowels in {} are {}".format(string,"".join(find_vowel)))
lower_bound+=1
You can also set higher_bound to len(string) then it will print result as many times as len of string.
Since this is your Study assignment you should look and practice yourself instead of copy paste. Here is additional info for solution :
In mathematics, the intersection A ∩ B of two sets A and B is the set
that contains all elements of A that also belong to B (or
equivalently, all elements of B that also belong to A), but no other
elements. For explanation of the symbols used in this article, refer
to the table of mathematical symbols.
In python :
The syntax of intersection() in Python is:
A.intersection(*other_sets)
A = {2, 3, 5, 4}
B = {2, 5, 100}
C = {2, 3, 8, 9, 10}
print(B.intersection(A))
print(B.intersection(C))
print(A.intersection(C))
print(C.intersection(A, B))
You can get the character at index x of a string by doing string[x]!
i = 0 # initialise i to 0 here first!
while i < len( string ):
if string[i] in vowels:
indices += str(i)
i += 1
print( indices )
However, is making indices a str really suitable? I don't think so, since you don't have separators between the indices. Is the string "12" mean that there are 2 vowels at index 1 and 2, or one vowel index 12? You can try using a list to store the indices:
indices = []
And you can add i to it by doing:
indices.append(i)
BTW, your for loop solution will print the vowel characters, not the indices.
If you don't want to use lists, you can also add an extra space after each index.
indices += str(I) + " "

How do I split a string with multiple separators in lua?

I want to split a string into an array divided by multiple delimiters.
local delim = {",", " ", "."}
local s = "a, b c .d e , f 10, M10 , 20,5"
Result table should look like this:
{"a", "b", "c", "d", "e", "f", "10", "M10", "20", "5"}
Delimiters can be white spaces, commas or dots.
If two delimiters like a white space and comma are coming after each other, they should be collapsed, additional whitespaces should be ignored.
This code splits the string as required by building a pattern of the complement of the delimiter set.
local delim = {",", " ", "."}
local s = "a, b c .d e , f 10, M10 , 20,5"
local p = "[^"..table.concat(delim).."]+"
for w in s:gmatch(p) do
print(w)
end
Adapt the code to save the "words" in a table.

How to reduce all whitespace in a string to single spaces?

Using VB6 without any additional references such as Regex, how can I convert a string so that all whitespace in a string is reduced to single spaces?
eg.
" A B C D E"
would be converted to
"A B C D E"
Function NormalizeSpaces(s As String) As String
Do While InStr(s, String(2, " ")) > 0
s = replace(s, String(2, " "), " ")
Loop
NormalizeSpaces = s
End Function
(derived from: http://www.techrepublic.com/article/normalizing-spaces-in-vb6-strings/5890164)

Resources