# New to Python...Getting Syntax Error

**URL:** <https://discuss.python.org/t/new-to-python-getting-syntax-error/16120>\
**Category:** Python Help\
**Created:** [May 31, 2022, 2:56pm UTC](https://discuss.python.org/t/new-to-python-getting-syntax-error/16120 "2022-05-31T14:56:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamainebarnes](https://avatars.discourse-cdn.com/v4/letter/j/f6c823/32.png) [@jamainebarnes](https://discuss.python.org/u/jamainebarnes)\
**Post date:** [May 31, 2022, 2:56pm UTC](https://discuss.python.org/t/new-to-python-getting-syntax-error/16120/1 "2022-05-31T14:56:20Z")

</div>

I am getting this error below.

SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape

It seems to have something to do with this line in the script…

source\_folder = Path(‘C:\Users\JB095808\OneDrive - Cerner Corporation\Documents\VA EHRM TO 0043\RLM Log Sample\SampleRLMLogs\Logs’)

Is my syntax incorrect?

---

<div class="post-metadata">

**Author:** ![vbrozik](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vbrozik/32/7423_2.png) [@vbrozik](https://discuss.python.org/u/vbrozik)\
**Post date:** [May 31, 2022, 3:03pm UTC](https://discuss.python.org/t/new-to-python-getting-syntax-error/16120/2 "2022-05-31T15:03:01Z")

</div>

The backslash character `\` has a special meaning in strings in Python and many other languages. It changes the meaning of the character(s) following it. You have to either escape it using `\` or use a raw string:

`r'C:\Users\JB095808\OneDrive - Cerner Corporation\Documents\VA EHRM TO 0043\RLM Log Sample\SampleRLMLogs\Logs'`

Also note that in your post you have incorrect quote characters around the string.

More information about string literals: [2. Lexical analysis — Python 3.10.4 documentation](https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals)

---

<div class="post-metadata">

**Author:** ![sandraC](https://avatars.discourse-cdn.com/v4/letter/s/8491ac/32.png) [@sandraC](https://discuss.python.org/u/sandraC)\
**Post date:** [May 31, 2022, 3:38pm UTC](https://discuss.python.org/t/new-to-python-getting-syntax-error/16120/3 "2022-05-31T15:38:23Z")

</div>

> Also note that in your post you have incorrect quote characters around the string.

It is probably a matter of Discourse. It re-formats some characters unless you do not use md code blocks.

---

<div class="post-metadata">

**Author:** ![mlgtechuser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mlgtechuser/32/7310_2.png) [@mlgtechuser](https://discuss.python.org/u/mlgtechuser)\
**Post date:** [June 4, 2022, 4:51am UTC](https://discuss.python.org/t/new-to-python-getting-syntax-error/16120/4 "2022-06-04T04:51:31Z")

</div>

To extend Václav’s reply, Jamaine, if backslash ( ’ **\** ’ ) is one of the character in a string, the default meaning of the backslash is “the following character is a control character” where a _ **control character** _ is a special code to make the interpreter do something more than just display a character in a string. You may have already seen ’ **\n** ’ used to trigger a Line Feed (go to next line) in a `print()` instruction like

```python
print("first line \n second line \n third line")

```

So with `source = Path('C:\Users')` Python’s interpreter expects that ‘U’ is a control code. However, there is no such control code, so the error message is telling you that ‘`\U`’ can’t be decoded as a control code. If you _ **happen to** _ follow the backslash with a valid control code, this can lead to some unexpected behavior like when you run something like `print("this\next week")`.

To use the backslash as a character in a string, you have to reassert the backslash so Python knows that ‘`\\`’ means “this is only a backslash, not a control code”. So your system paths in Python will look like ‘`C:\\Users\\...`’

BTW, this “escape character” is found in many programming languages. In fact, this forum platform (Discourse) uses it. Here’s a useful example: When you paste code here, you should “fence” it with three backticks ( ``` ) as the first and last lines. Since three backticks are a control code to trigger `the monotext font`, I had to precede the backticks in the previous sentence with a backslash to “escape out” of the default behavior in order to just print the backticks.

The ``` fence also has other useful features like preserving indents and suppressing curved quotation marks. So next time you paste code, it will look like this in your raw post text:

**```python  
\<your code here\>  
```**  
The ‘python’ on the first line activates color coding so that the code components are easier to track.

```python
source_folder = Path('C:\Users\JB095808\...')

```

P.S. These text format control codes are called “Markdown”. (@epicwink abbreviated it as ‘md’, in case you didn’t recognize this abbreviation). A full treatment of the Commonmark markdown used by Discourse (and other platforms, like WhatsApp) **[CAN BE FOUND HERE](https://commonmark.org/help/). [AND ALSO HERE](https://spec.commonmark.org/current/)**

---

<div class="post-metadata">

**Author:** ![vbrozik](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vbrozik/32/7423_2.png) [@vbrozik](https://discuss.python.org/u/vbrozik)\
**Post date:** [June 6, 2022, 8:25am UTC](https://discuss.python.org/t/new-to-python-getting-syntax-error/16120/5 "2022-06-06T08:25:39Z")

</div>

@mlgtechuser

> [@mlgtechuser](#):
>
> Python’s interpreter expects that ‘U’ is a control code. However, there is no such control code

Just a small correction: `\U` has a special meaning in Python. It marks Unicode 32-bit codepoint in hexadecimal. See [The String Type](https://docs.python.org/3/howto/unicode.html#the-string-type) in the documentation. That is the reason the error message contained: `(unicode error) ‘unicodeescape’ codec can’t decode bytes` …

Here is an example how you can write the letter `A` multiple ways (32b, 16b, 8b):

```auto
>>> '\U00000041 == \u0041 == \x41 == A'
'A == A == A == A'

```

Edit: Non-existing escape sequences do not cause errors. They have the literal meaning:

```auto
>>> '\z'
'\\z'

```

---

<div class="post-metadata">

**Author:** ![mlgtechuser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mlgtechuser/32/7310_2.png) [@mlgtechuser](https://discuss.python.org/u/mlgtechuser)\
**Post date:** [June 6, 2022, 12:25pm UTC](https://discuss.python.org/t/new-to-python-getting-syntax-error/16120/6 "2022-06-06T12:25:29Z")

</div>

Thanks for clarifying this, Václav. I tested `\U` before posting but haven’t used an escaped unicode before.

So the issue is that what folllows the `\U` isn’t a unicode value.

And this example might illustrate a non-existent escape code a little clearer (both are informative):

```nohighlight
>>>print("\c")
\c
```

---

<div class="post-metadata">

**Author:** ![eryksun](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/eryksun/32/697_2.png) [@eryksun](https://discuss.python.org/u/eryksun)\
**Post date:** [June 6, 2022, 1:08pm UTC](https://discuss.python.org/t/new-to-python-getting-syntax-error/16120/7 "2022-06-06T13:08:44Z")

</div>

The error message states “truncated \UXXXXXXXX escape” at position 2-3 in `"C:\Users..."`. How can this be improved to make it clear that 8 hexadecimal (“X”) digits are expected after `"\U"` in the string literal? Does Python need error codes for its syntax errors that can be looked up online or via `help()`? Or references to sections of the documentation?

---

<div class="post-metadata">

**Author:** ![vbrozik](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vbrozik/32/7423_2.png) [@vbrozik](https://discuss.python.org/u/vbrozik)\
**Post date:** [June 6, 2022, 2:15pm UTC](https://discuss.python.org/t/new-to-python-getting-syntax-error/16120/8 "2022-06-06T14:15:49Z")

</div>

@eryksun I think that ability to go from an error message to the most useful place in the documentation would be extremely useful for beginners. Error codes could probably be a solution…

Also ability to go from a standard library object in REPL, IPython, vim, VS Code… to the documentation would be very useful.

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [June 6, 2022, 3:30pm UTC](https://discuss.python.org/t/new-to-python-getting-syntax-error/16120/9 "2022-06-06T15:30:32Z")

</div>

If beginners won’t google for error messages, they certainly won’t google for error codes.

[https://www.startpage.com/sp/search?query=truncated+\UXXXXXXXX+escape](https://www.startpage.com/sp/search?query=truncated+%5CUXXXXXXXX+escape)

> **[truncated \\UXXXXXXXX escape at DuckDuckGo](https://duckduckgo.com/?q=truncated%2B%5CUXXXXXXXX%2Bescape)**
>
> DuckDuckGo. Privacy, Simplified.
