# Concatenating string and atom

**URL:** <https://swi-prolog.discourse.group/t/concatenating-string-and-atom/1952>\
**Category:** Help!\
**Created:** [March 3, 2020, 3:45pm UTC](https://swi-prolog.discourse.group/t/concatenating-string-and-atom/1952 "2020-03-03T15:45:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [March 3, 2020, 3:45pm UTC](https://swi-prolog.discourse.group/t/concatenating-string-and-atom/1952/1 "2020-03-03T15:45:49Z")

</div>

Hello,

I need to concatenate a string with two atoms to obtain a hybrid string, such as:

```prolog
A = "a"
B = '!'
C = label

```

and the end result should be:

```prolog
"a"!label

```

I guess this could be an atom as well

```prolog
'"a"!label'

```

but not:

```prolog
\"a\"!label

```

or

```prolog
'\"a\"!label'

```

somehow this eludes me, i can concatenate atoms or strings but how is it done for a combination …

any thoughts are much appreciated,

Dan

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [March 3, 2020, 4:00pm UTC](https://swi-prolog.discourse.group/t/concatenating-string-and-atom/1952/2 "2020-03-03T16:00:20Z")

</div>

```prolog
?- atomic_list_concat(['"',"a",'"','!',label],Result),atom(Result).
Result = '"a"!label'.

```

Does that work?

**EDIT**

If you prefer a string

```prolog
?- atomics_to_string(['"',"a",'"','!',label],Result),string(Result).
Result = "\"a\"!label".

```

---

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [March 3, 2020, 4:02pm UTC](https://swi-prolog.discourse.group/t/concatenating-string-and-atom/1952/3 "2020-03-03T16:02:38Z")

</div>

Thanks, Let me try …

Its somehow also complicated by how ~q and ~w are interpreted in a format string.

I noticed that if i pass both parts into format and use for one ~q and the other ~w and let it generate a string(S), then it seems to work.

I will also try your suggestion, so i can avoid a separate case.

thanks,  
Dan

---

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [March 3, 2020, 4:11pm UTC](https://swi-prolog.discourse.group/t/concatenating-string-and-atom/1952/4 "2020-03-03T16:11:08Z")

</div>

No, it didn’t work.

Its some quirk how ~q or ~w process the concatenated hybrid … seems to fall in-between the chairs. I need to process the string part with ~q and the atom part with ~w, so now i added another predicate where i pass the string and atom as to args, and format does the concatenation with the correct “format code”.

Seems to work that way  
Dan
