# Update string with variables

**URL:** <https://discourse.julialang.org/t/update-string-with-variables/52024>\
**Category:** New to Julia\
**Created:** [December 18, 2020, 5:35am UTC](https://discourse.julialang.org/t/update-string-with-variables/52024 "2020-12-18T05:35:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [December 18, 2020, 5:35am UTC](https://discourse.julialang.org/t/update-string-with-variables/52024/1 "2020-12-18T05:35:05Z")

</div>

I need help on a very simple problem updating a string.

In **Python** I could do the following:

```julia
DEFAULT_STRING = 'https://...{}/{}'
DEFAULT_STRING.format("foo","bar")

```

giving `https://...foo/bar`

In **Julia** I am not able to do this: the following gets me halfway there…

```julia
using Formatting
DEFAULT_STRING = FormatExpr("https://...{}/{}")
url = printfmt(DEFAULT_STRING, "foo", "bar")

```

Two problems rg. my “solution”:

1. only prints the string, but returns nothing (`typeof(url)`)
2. requires installing and using a specific package

**Question:** How can I achieve this in a simple way without installing a package?

Thanks for your suggestions!

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [December 18, 2020, 12:21pm UTC](https://discourse.julialang.org/t/update-string-with-variables/52024/2 "2020-12-18T12:21:19Z")

</div>

Is it important you follow exactly the same convention of creating a default string with placeholder spots and then fill those in later?

If not, you could simply use `joinpath` or `join`.

```julia
some_url(a, b) = joinpath("https://...", a, b)

```

If it _is_ vitally important to follow that convention, you could create your own string type that has a `format` method defined on it that replaces placeholder tokens (e.g. `{}`) with whatever else. It wouldn’t be too hard to do (probably what Formatting does as well) but seems like more trouble that it’s worth.

You could also do the formatting manually with `findfirst/next` and `replace`…

---

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [December 18, 2020, 12:36pm UTC](https://discourse.julialang.org/t/update-string-with-variables/52024/3 "2020-12-18T12:36:30Z")

</div>

Thanks for your response.

This doesn’t work as the placeholder may be anywhere within a string. This was just an example.

Any other ideas? I cannot imagine that this is a difficult problem to solve in Julia. But I just cannot find anything…

Thanks

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [December 18, 2020, 12:57pm UTC](https://discourse.julialang.org/t/update-string-with-variables/52024/5 "2020-12-18T12:57:16Z")

</div>

`replace` works with regular expressions, so with a small difference (i.e. labelling the different placeholders):

```julia
pat = "https://...{1}/{2}"
replacements = ("foo", "bar")
replace(pat, r"{\d}" => m -> replacements[parse(Int, m[2:end-1])])

```

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [December 18, 2020, 12:57pm UTC](https://discourse.julialang.org/t/update-string-with-variables/52024/6 "2020-12-18T12:57:27Z")

</div>

> [@ZettEff](#):
>
> Any other ideas? I cannot imagine that this is a difficult problem to solve in Julia. But I just cannot find anything…

It’s not; Formatting.jl does it in a really general way too that duplicates all of pythons formatting options. If you don’t want to use any dependency whatsoever, you could always lift parts of their source code into your own module, or base a simpler version (for interpolation only) on it. Check out [the source file](https://github.com/JuliaIO/Formatting.jl/blob/master/src/formatexpr.jl) for ideas.

You could slightly generalize the joinpath solution I gave if you want different strings and different interpolation spots. It’s still as rigid and inflexible as you can possibly get though:

```julia
url1 = (a, b) -> joinpath("https://...", a, b)
url2 = a -> "https://discourse.julialang.org/" * a * "/baz"
url3 = (a...) -> joinpath("etc.", a...)

url1("foo", "bar")
url2("here")
url3("etc", "etc", "etc")

```

---

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [December 18, 2020, 12:59pm UTC](https://discourse.julialang.org/t/update-string-with-variables/52024/7 "2020-12-18T12:59:29Z")

</div>

Thanks. Could you give me an example how to do this using Formatting. My solution (see above) does not work.

Thanks!

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [December 18, 2020, 1:00pm UTC](https://discourse.julialang.org/t/update-string-with-variables/52024/8 "2020-12-18T13:00:45Z")

</div>

The easiest and imho cleanest way is to define a function:

```julia
get_url(x1, x2) = "https://...$x1/$x2"

url = get_url("foo" , "bar")

```

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [December 18, 2020, 1:01pm UTC](https://discourse.julialang.org/t/update-string-with-variables/52024/9 "2020-12-18T13:01:24Z")

</div>

Would something like this work for you?

```julia
foo="foo"
bar="bar"
url="https://../$(foo)/$(bar)"

```

I’m not exactly sure what you have against the package Formatting? Why don’t you want to use external packages? Because this seems like exactly what you want:

```julia
using Formatting

DEFAULT_STRING = FormatExpr("https://...{}/{}")
url = format(DEFAULT_STRING, "foo", "bar")

```

If you really don’t want to use an “external” package Formatting.jl is open source, you could go find the specific methods you actually need and put them into your own Julia file…then it’s not an external package. The files are licensed under the MIT “Expat” license so I think that would be legal.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [December 18, 2020, 1:03pm UTC](https://discourse.julialang.org/t/update-string-with-variables/52024/10 "2020-12-18T13:03:22Z")

</div>

Ah I did not see your original problem with the string vs print! Whenever you encounter something like that, you can use `sprint` to capture the printout as a string (it’s quite a useful trick!)

```julia
using Formatting
DEFAULT_STRING = FormatExpr("https://...{}/{}")

julia> sprint(printfmt, DEFAULT_STRING, "foo", "bar")
"https://...foo/bar"

```

---

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [December 18, 2020, 1:03pm UTC](https://discourse.julialang.org/t/update-string-with-variables/52024/11 "2020-12-18T13:03:53Z")

</div>

> [@lungben](#):
>
> ```julia
> get_url(x1, x2) = "https://...$x1/$x2"
> 
> url = get_url("foo" , "bar")
> 
> ```

Thanks!! This is a solution that intuitively makes sense and perfectly works for my purposes.

---

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [December 18, 2020, 1:04pm UTC](https://discourse.julialang.org/t/update-string-with-variables/52024/12 "2020-12-18T13:04:57Z")

</div>

Ahh…so easy. Thanks! Although I must admit that I find @lungben solution more concise and intuitive (plus I don’t need number formatting etc.).

Thanks for your help!
