I have a list genus, containing the list of genus group names for some fishes I work on derived from a column of imported data:

genus = tempData2[[All,2]] (* here explicitly specifying just the 4 of them for brevity*)

returning the list

{Synanciea, Pterois, Helicolenus, Sebastes}

If I examine the head using


it returns List as expected

If I examine the contents of the list using

Table[Head[genus[[i]], {i, 1, Length[genus]}] (*fudging here that only 4 are used*)

it returns, as expected

{String, String, String, String}

I wish to simply take the individual strings and append a " " to each string as follow

genusname = Map[ReplaceAll[#&, #-> #<>" "],genus]

This works as intended returning appending the blank character onto each original string in the list:

{Synancia , Pterois , Helicolenus , Sebastes }

or {"Synancia ","Pterois ", "Helicolenus ", "Sebastes "} (when //InputForm is used in postfix notation)

However, it also generates the Error ... "StringJoin: String expected at position 1 in #1"

If I try to explicitly convert the slot to String as follows

Map[ReplaceAll[# &, ToString[#]-> ToString[#]<>" "], genus]

it returns no error, but it doesn't append the blank character at the end of each string in the list.

Obviously, I can suppress/ignore the error since the first effort works, but I am not sure why it occurs.
Can anyone explain why the first effort at Map returns the error but works, whereas the second effort of Map does not return an error but does not work (leaves each element of the list unchanged)?


3 Answers 3


First, I think you have a typo in your question. This:

genusname = Map[ReplaceAll, # &, # -> # <> " "], genus]

is not valid Wolfram Language syntax, you probably meant to write:

genusname = Map[ReplaceAll[# &, # -> # <> " "], genus]

If you do this in a notebook interface you will see that some of the slots (#) are purple, indicating that they are not bound to a Function.

You can sort of fix that by placing the Function shorthand operator (&) at the end:

genusname = Map[ReplaceAll[#, # -> # <> " "] &, genus]

which makes all the #'s green (green is good!) and gives you the answer you want.

But a simpler way it to use this:

Map[# <> " " &, genus]

which takes each string in turn and uses StringJoin (<>) to add a space character.

  • 1
    $\begingroup$ Green is always good. Eventually, I'll figure out anonymous functions. Thanks. $\endgroup$ Apr 17, 2020 at 22:08
  • 1
    $\begingroup$ Typos corrected in original post. $\endgroup$ Apr 17, 2020 at 22:20
  • $\begingroup$ You can always write anonymous functions with, for example Function[#^2]. That way you don't have to worry about where to put the & and if it needs parenthetical grouping. And the code becomes slightly more readable since it isn't a big ASCII soup of characters. $\endgroup$ Apr 17, 2020 at 23:44

I wish to simply take the individual strings and append a " " to each string

One possibility is to use StringInsert

  list = {"Synanciea", "Pterois", "Helicolenus", "Sebastes"};
  StringInsert[#, " ", -1] & /@ list


  {"Synanciea ", "Pterois ", "Helicolenus ", "Sebastes "}
ReplaceAll[genus, s_String :> s <> " "] (* or *)

ReplaceAll[s_String :> s <> " "] @ genus
 {"Synanciea ", "Pterois ", "Helicolenus ", "Sebastes "}


StringReplace[EndOfString -> " "] @ genus
{"Synanciea ", "Pterois ", "Helicolenus ", "Sebastes "}
StringReplace[genus, s__ :> s <> " "] 
{"Synanciea ", "Pterois ", "Helicolenus ", "Sebastes "}
StringPadRight[#, 1 + StringLength@#] & /@ genus
{"Synanciea ", "Pterois ", "Helicolenus ", "Sebastes "}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.