How to use a character '# in string?
Question:
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?
Answers:
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
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"!@#$%^&*()nt\{a}"
formatted_raw_string = fr"!@#$%^&*()nt\{a}"
print("Raw String = ", raw_string)
print("Formatted Raw String = ", formatted_raw_string)
Output:
Raw String = !@#$%^&()nt{a}
Formatted Raw String = !@#$%^&()nt100
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?
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
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"!@#$%^&*()nt\{a}"
formatted_raw_string = fr"!@#$%^&*()nt\{a}"
print("Raw String = ", raw_string)
print("Formatted Raw String = ", formatted_raw_string)
Output:
Raw String = !@#$%^&()nt{a}
Formatted Raw String = !@#$%^&()nt100