I am using a platform which uses a JavaScript-like syntax and allows to convert my values using regex.
I need to add a single quote at the beginning and at the end of a string, the string will always have 9 characters.
I.e.:
123456789
should be converted to '123456789'
What expression should I use?
Method 1: By using +: We can add a character to the end or start of a string by using +.
Example: One can add character at the start of String using the '+' operator.
Add a Character to a String in Python Using the + Operator The + operator can concatenate two strings or a string and a character and returns a new string in Python.
Insert a character at the beginning of the String using the + operator. Insert a character at the end of the String using the + operator.
You don't need a regular expression.
str = "123456789"; str = "'" + str + "'";
Although this question was asked a little early, newly defined backticks
standard called Template Literals from ES6 provides new possibility to solve this question with a simpler syntax:
const str = '123456789'; const wrapped = `'${str}'`;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With