Home > Back-end >  How to use a character '# in string?
How to use a character '# in string?

Time:11-08

I am trying to conncect MSSQL DB to python, and the DB's password has included a character '#'.

Is there any way to use '#' in string, not changing the password?

CodePudding user response:

You can use the character '#' in a string in python programming language by using the escape character ''.

For example:

print("#include <stdio.h>")

You can use the character '#' in a string in Python by putting it inside quotes:

my_string = "This is a string with a # in it

CodePudding user response:

As somebody has already answered the question, you can use r modifier for raw string which will basically ignore the special character string functionalities.

You can also use fr modifier as a combination of formatted and raw string in python. Find the below example for your reference.

a = 100
raw_string = r"!@#$%^&*()\n\t\\{a}"
formatted_raw_string = fr"!@#$%^&*()\n\t\\{a}"
print("Raw String = ", raw_string)
print("Formatted Raw String = ", formatted_raw_string)

Output:
Raw String = !@#$%^&()\n\t\{a}
Formatted Raw String = !@#$%^&
()\n\t\100

  • Related