{"id":2962,"date":"2023-10-17T10:30:13","date_gmt":"2023-10-17T05:00:13","guid":{"rendered":"https:\/\/python-programs.com\/?p=2962"},"modified":"2023-11-10T11:43:46","modified_gmt":"2023-11-10T06:13:46","slug":"basics-of-python-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/basics-of-python-string\/","title":{"rendered":"Basics of Python \u2013 String"},"content":{"rendered":"

In this Page, We are Providing Basics of Python \u2013 String. Students can visit for more Detail and Explanation of Python Handwritten Notes<\/a>\u00a0Pdf.<\/p>\n

Basics of Python \u2013 String<\/h2>\n

String<\/strong><\/p>\n

Python can manipulate string, which can be expressed in several ways. String literals can be enclosed in matching single quotes (‘) or double quotes (“); e.g. ‘hello’, “hello” etc. They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple- quoted strings), e.g. ‘ hello ‘, “hello”. A string is enclosed in double-quotes if the string contains a single quote (and no double quotes), else it is enclosed in single quotes.<\/p>\n

>>> \"doesnt\"\r\n'doesnt'\r\n>>> \"doesn't\"\r\n\"doesn't\"\r\n>>> '\"Yes,\" he said.'\r\n' \"Yes., \" he said. '<\/pre>\n

The above statements can also be written in some other interesting way using escape sequences.<\/p>\n

>>> \"doesn\\' t\" ;\r\n\"doesn ' t\u201d\r\n\u2018>>> '\\\"Yes,\\\" he said.'\u00a0\r\n' \"Yes,\" he said.'<\/pre>\n

Escape sequences are character combinations that comprise a backslash (\\) followed by some character, that has special meaning, such as newline, backslash itself, or the quote character. They are called escape sequences because the backslash causes an escape from the normal way characters are interpreted by the compiler\/interpreter. The print statement produces a more readable output for such input strings. Table 2-12 mentions some of the escape sequences.<\/p>\n\n\n\n\n\n\n
\n

Escape sequence<\/p>\n<\/td>\n

Meaning<\/td>\n<\/tr>\n
\n

\\n<\/p>\n<\/td>\n

Newline.<\/td>\n<\/tr>\n
\\t<\/td>\n\n

Horizontal tab.<\/p>\n<\/td>\n<\/tr>\n

\\v<\/td>\n\n

Vertical tab.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

 <\/p>\n\n\n\n\n\n\n
\n

Escape sequence<\/p>\n<\/td>\n

Meaning<\/td>\n<\/tr>\n
W<\/td>\n\n

A backslash (\\).<\/p>\n<\/td>\n<\/tr>\n

\n

Y<\/p>\n<\/td>\n

Single quote (‘).<\/td>\n<\/tr>\n
\\”<\/td>\n\n

Double quote (“).<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

String literals may optionally be prefixed with a letter r or R, such string is called raw string, and there is no escaping of character by a backslash.<\/p>\n

>>> str=' This is \\n a string '\r\n>>> print str \r\nThis is \r\na string\r\n>>> str=r' This is \\n a string '\r\n>>> print str \r\nThis is \\n a string<\/pre>\n

Specifically, a raw string cannot end in a single backslash.<\/p>\n

>>> str=r ' \\ '\r\nFile \"<stdin>\", line 1\u00a0\r\nstr=r ' \\ '\r\n            \u2227\r\nSyntaxError: EOL while scanning string literal<\/pre>\n

Triple quotes are used to specify multi-line strings. One can use single quotes and double quotes freely within the triple quotes.<\/p>\n

>>> line=\" \" \"This is\r\n. . . a triple\r\n. . . quotes example\" \" \"\r\n>>> line\r\n' This, is\\na triple\\nquotes example'\r\n>>> print line\r\nThis is\r\na triple \r\nquotes example<\/pre>\n

Unicode strings are not discussed in this book, but just for the information that a prefix of ‘ u ‘ or ‘ U’ makes the string a Unicode string.<\/p>\n

The string module contains a number of useful constants and functions for string-based operations. Also, for string functions based on regular expressions, refer to re module. Both string and re modules are discussed later in this chapter.<\/p>\n

Python String Programs<\/h3>\n