For those that are coming here while googling something like "python surround string" and are time conscientious (or just looking for the "best" solution). Show
I was going to add in that there are now f-strings which for Python 3.6+ environments are way easier to use and (from what I read) they say are faster.
I decided to do a timeit of each method of "surrounding" a string in python.
Results:
Oddly enough, I'm getting that concatenation is faster than f-string every time I run it, but you can copy and paste to see if your string/use works differently, there may also be a better way to put them into timeit than Try it online! Printing double quotes is tricky, as it itself is required as part of syntax to print the strings by surrounding them. In this article we will see how these double quotes can be printed using print statement. The below scenarios will not print the double quote. The first two lines of code will give no output while the last one will through error. ExampleLive Demo print(" ") print(" " " ") print(""aString"") OutputRunning the above code gives us the following result −; print(""aString"") ^ SyntaxError: invalid syntax But if we surround the strings with proper quotes as shown below, then the quotes can themselves get printed. Enclosing double quotes within single quotes does the trick. ExampleLive Demo print('Hello Tutorialspoint') print('"Hello Tutorialspoint"') OutputRunning the above code gives us the following result − Hello Tutorialspoint "Hello Tutorialspoint" Using string variablesWe can also use string formatting to print the double quotes as well as any other character which is part of the print syntax. ExampleLive Demo StringVar = 'Hello Tutorialspoint' print("\"%s\""% StringVar ) print("\%s\"% StringVar ) print('"%s"' % StringVar ) print('"{}"'.format(StringVar)) OutputRunning the above code gives us the following result − "Hello Tutorialspoint" \Hello Tutorialspoint\ "Hello Tutorialspoint" "Hello Tutorialspoint"
Updated on 26-Feb-2020 07:54:40
You are welcome to go through our tutorials but please keep in mind that as we rely on community PRs for maintainance they may be out of date. In this tutorial we are going to look at variables, user input and decision making. Python allows us to store data in something called variables so that we are able to use this data at a later point. To place an item in a variable we give it a name then set its value. Now in the REPL type: In this example you have now stored the value How about saving your age into a variable or your lucky number? Have a play around with storing numbers into variables. Now that you are familiar with the use of variables, we are able to combine variables with the maths operations we learnt in the previous tutorial. Now in the REPL type the following:
Now type Now work out the cost of running a codebar workshop if 60 people turned up and pizza cost £8 per 2 people? Along with pizza, students and cost, what other variables can you think of that could go into this calculation? Storing text in variablesAs well as numbers variables are able to store text, known in Python as strings. Now in the REPL type:
Now type For example: Sometimes you will need to use an apostrophe within a single quote, on occasions like this it is recommended to use “string escaping”. This would look like: Try storing a string within a variable without quotes, see what happens? Numbers do not require quotation marks, whereas they are mandatory for storing strings. Now store some strings in variables that contain apostrophes and some that do not. Types & castingPython is what’s called a “typed language”. This is to say that there are multiple types of objects that you work with in Python, and they don’t all act the same way. The three types you’ve learnt so far are integers ( Go ahead and try this in your REPL, it explodes with a This tells Python to multiply a string by 8 so you get eight sevens rather than what you might expect. This is actually a very powerful feature of Python, but when you’re just starting out, it can be pretty confusing. For now, all you really need to know is that if you have a string that you mean to treat like a number, you have to cast it that way. This is a string: While this is a number: This is an integer: While this is a string: Go ahead an experiment with Storing user input in variablesNow we are going to look at capturing user input using the python input command. Let’s create a variable in which to store the user input. Now type this into your REPL:
Type back your answer after it asks you. Now in the REPL type:
Now we are going to put your response into another variable. Now try:
Then type Decision making using variablesNow that we know how to use variables and know how to store data, let’s play around with decision making and changing prints based on your answer. In Python (and many other languages), one of the most common ways in which this is done is using an
Here we can see that if a number we have passed into this decision making code is bigger than three, we will receive a message telling us so. There is a different message if the number is smaller than three. Also, now that we are getting more in depth with Python, we should say that Python is very particular about indentation (i.e. the spaces at the start of a line of code). With Python, if any lines are not indented correctly the code will not run. If you are running into bugs, this is a good place to start. In this final exercise we are going to ask you the number of coffees you have drunk today and then change the statement returned to you, depending on your answer. Let’s create a variable called
Now we’ll use some simple if/else logic to decide what to say about your drinking habits:
Note the use of How do you put a variable in a quote in Python?For Python, you might use '"' + str(variable) + '"' .
How do you put a variable in a quote?When referencing a variable, it is generally advisable to enclose its name in double quotes. This prevents reinterpretation of all special characters within the quoted string -- except $, ` (backquote), and \ (escape).
How do you print a double quote variable?Using \" escape sequence in printf, we can print the double quotes ("").
How do you pass a string with double quotes?If you need to use the double quote inside the string, you can use the backslash character. Notice how the backslash in the second line is used to escape the double quote characters. And the single quote can be used without a backslash.
|