How do i use basic strings in python?
-
Python, use strings like other programming & scripting languages. The more you use the more you will notice the similarities.
a = "This is a double quote string" b = 'This is a single quote string' print(a) print(b)
Ans: Prints the same thing.
This is a double quote string
This is a single quote stringTo print double quotes within the string, use single quotes.
d = 'This is a double "quote" string' print(d)
Ans:
This is a double "quote" stringOther ways of printing the actual double quotes are to use backslashes, like this:
"quote".A multi-line string can be done using the backslash too, the line is printed without line-breaks.
line = "This is the output from the server logs \ the colour coding shows the different states" print(line)
Ans: This is the output from the server logs the colour coding shows the different states
Strings with methods & concatenation
location = "madrid" locationletter = location[6] print(locationletter)
Ans: d
Note: The following was done from the interactive python prompt.
Make everything in variable mystring into lowercase.>>> mystring = "This a ScenTence" >>> print(mystring.lower()) this a scentence
Make everything in variable mystring into uppercase.
>>> print(mystring.upper()) THIS A SCENTENCE
Count the number of character in the string (including spaces).
>>> print(len(mystring)) 16
Append a string onto another string.
>>> print(mystring + str(2)) This a ScenTence2
Concatenate, here we concatenate words and spaces.
>>> print("hello " + " " + "world!") hello world!
Concatenation first letter of city, a space, and then the name of the city.
>>> print(locationletter + " " + city) g madrid >>>
Getting part of a string (splicing). The following includes spaces and zero is the first number in the index.
Set a string variable.
>>> mystring = "The rain in Spain stays mainly on the plain"
Using a number range and step number, which has the format of:
[range:step]
Print characters 1 through 5.
>>> mystring[1-5] 'he r'
Print last five characters from the end of the line.
>>> mystring[-5:] 'plain'
Print everything but the last three characters.
>>> mystring[:-3] 'The rain in Spain stays mainly on the pl'
Here's an example of step, even though it says step 2, essentially it means skip every one character. This example steps every 2 characters, starting from zero, which is the "T" to get this garbled text.
>>> mystring[::2] 'Teri nSansasmil ntepan'
String formatting.
Before we used this type of syntax to put a string together, using pluses. country and place are variables.
>>> country = Iceland >>> place = plain >>> print("The rain in" + " " + country + " " "stays mainly on the" + " " + place) The rain in Iceland stays mainly on the plain
There's a better way using string formatting, easier to read, etc. country and place are variables. The %s entries are substituted for the variable values.
>>> print("The rain in %s stays mainly on the %s" % (country, place)) The rain in Iceland stays mainly on the plain
© Lightnetics 2024