Text in Python | Text Data Type | Python Data Type | Python String | String in Python |
Python can manipulate text (represented by type str, so-called “strings”) as well as numbers. This includes characters “!”, words “rabbit”, names “Paris”, sentences “Got your back.”, etc. “Yay! :)”. They can be enclosed in single quotes ('...') or double quotes ("...") with the same result. print("Hello ! :) Welcome Here") print('Hello ! :) Welcome Here') print('2023') print(2023) print(2020) print('T20') To quote a quote, we need to “escape” it, by preceding it with \. Alternatively, we can use the other type of quotation marks: print('doesn\'t') print("doesn\'t") print('"Yes," they said.') print("\"Yes,\" they said.") print('"Isn\'t," they said.') In the Python shell, the string definition and output string can look different. The print() function produces a more readable output, by omitting the enclosing quotes and by printing escaped and special characters: s = 'First line.\nSecond line.' # \n means newline s # without print(), special characters are included in the string 'First line.\nSecond line.' print(s) # with print(), special characters are interpreted, so \n produces new line First line. Second line. If you don’t want characters prefaced by \ to be interpreted as special characters, you can use raw strings by adding an r before the first quote: print('Devvrat\nKnowledgevilla') name = r'C:\some\name' #raw string python print(name) There is one subtle aspect to raw strings: a raw string may not end in an odd number of \ characters; see the FAQ entry for more information and workarounds. String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''. End of lines are automatically included in the string, but it’s possible to prevent this by adding a \ at the end of the line. The following example: print(""" Python can manipulate text (represented by type str, so-called “strings”) as well as numbers. This includes characters “!”, words “rabbit”, names “Paris”, sentences “Got your back.”, etc. “Yay! :)”. They can be enclosed in single quotes ('...') or double quotes ("...") with the same result. """) Strings can be concatenated (glued together) with the + operator, and repeated with *: print(3 * "dev""vrat") print(3 * "dev"+"vrat") Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated. print("dev""vrat") If you want to concatenate variables or a variable and a literal, use +: name = "dev" print(name +'vrat') This feature is particularly useful when you want to break long strings: text = ('Put several strings within parentheses ' 'to have them joined together.') print(text) This only works with two literals though, not with variables or expressions: prefix = 'Py' prefix 'thon' # can't concatenate a variable and a string literal File "stdin", line 1 print(prefix 'thon') ^^^^^^ SyntaxError: invalid syntax ('un' * 3) 'ium' File "stdin", line 1 ('un' * 3) 'ium' ^^^^^ SyntaxError: invalid syntax
Download
0 formatsNo download links available.