Python Programming/Strings Tutorial

String

There are three ways you can declare a string in Python: single quotes ('), double quotes ("), and triple quotes (""").

You can use quotation marks within strings by placing a backslash directly before them, so that Python knows you want to include the quotation marks in the string, instead of ending the string there. Placing a backslash directly before another symbol like this is known as escaping the symbol. Note that if you want to put a backslash into the string, you also have to escape the backslash, to tell Python that you want to include the backslash, rather than using it as an escape character.

 

>>> print("So I said, \"You don't know me! You'll never understand me!\"")

So I said, "You don't know me! You'll never understand me!"

>>> print("This will result in only three backslashes: \\ \\ \\")

This wwill result in only three backslashes: \ \ \

 

You can also add two strings together using the+ operator: this is called concatenating them.

>>> print("Hello, " + "world!")

Hello, world!

You can also repeat strings by using the* operator, like so:

>>> print("Amit, " * 25)

Amit,Amit,Amit,Amit,Amit,Amit,Amit,Amit,Amit,Amit,Amit,Amit,Amit,Amit,Amit,Amit,Amit,Amit,

Amit,Amit,Amit,Amit,Amit,Amit,Amit,

If you want to find out how long a string is, you use thelen() function, which simply takes a string and counts the number of characters in it. (lenstands for “length.”) Just put the string that you want to find the length of, inside the parentheses of the function.

>>> print(len("Hello, world!"))

13

Strings and Variables

question = "What did you have for lunch?"

print (question)

answer = input()

print ("You had " + answer + "! That sounds delicious!")

If we put in quotation marks around question, Python would treat it as a string, and simply print out question instead of What did you have for lunch?.

Combining Numbers and Strings

print ("Please give me a number: ")

number = input()

plusTen = number + 10

print ("If we add 10 to your number, we get " + plusTen)
Output

Please give me a number: 

21

Traceback (most recent call last):

 File "/home/Aps/string_number.py", line 4, in <module>

 plusTen = number + 10

TypeError: can only concatenate str (not "int") to str

What’s going on here? Python is telling us that there is a TypeError, which means there is a problem with the types of information being used. Specifically, Python can’t figure out how to reconcile the two types of data that are being used simultaneously: integers and strings.

Luckily, there are two functions that are perfect solutions for these problems. Theint() function will take a string and turn it into an integer, while the str() function will take an integer and turn it into a string. In both cases, we put what we want to change inside the parentheses. Therefore, our modified program will look like this:

 

print ("Please give me a number: ")

response = input()

number = int(response)

plusTen = number + 10

print ("If we add 10 to your number, we get " + str(plusTen))

Note:

Another way of doing the same is to add a comma after the string part and then the number variable, like this:

print ("If we add 10 to your number, we get ", plusTen)

or use special print formatting like this:

print ("If we add 10 to your number, we get %s" % plusTen)

which alternative can be written this way, if you have multiple inputs:

plusTwenty = number + 20
print ("If we add 10 and 20 to your number, we get %s and %s" % (plusTen, plusTwenty))


or use format()

print ("If we add 10 to your number, we get {0}".format(plusTen))

you can use the end argument to the print() function to prevent a newline character from being printed:

print("Nope, that is not a two. That is a", end="")

Source –> Think Python &  https://en.wikibooks.org

Leave a Reply