String and Characters



In Python, a string is a sequence of characters, and both string and character are considered the same. Python does not have a data type with respect to characters. Therefore, we can use a single-character string for characters.

A string is a sequence of characters and can include text and numbers. String values must be enclosed in matching single quotes 'I am a string' or double quotes "I am also a string".
  
 	a = 'I am a string'
  	b = "I am also a string"
  
  

Encoding

As computers only recognize binary codes (i.e., a sequence of 0/1s), a character must be converted into binary numbers in a computer. Mapping a character to its binary representation is called character encoding

There are different ways to encode a character such as ASCII (American Standard Code for Information Interchange) and Unicode. ASCII encodes 128 specified characters into seven-bit integers which you can find from the ASCII chart. Unicode is an encoding scheme for representing international characters. A Unicode starts with \u, followed by four hexadecimal digits that run from \u0000 to \uFFFF. 

As Python supports Unicode and you can try to print some Unicode characters.
  
  >>> print(u'\u6B22\u8FCE')
  欢迎
  
  
  
  >>> print(u'\u011f')
  ğ
  
  
Python's ord() function takes the string argument of a single Unicode character and return its integer Unicode code decimal value.
  
  >>> ord('ğ')
  287
  
  

Concatenation

You can concatenate two strings in Python simply using +, e.g., "welcome" + " to my blog".

  >>> a = 'welcome' + ' to my blog'
  >>> print(a)
  welcome to my blog
  


Why do we need both single and double quotes

You can concatenate two strings in Python simply using +, e.g., "welcome" + " to my blog".

>>> print('Alice says 'hello' to Bob')
  File stdin, line 1
    print('Alice says 'hello' to Bob')
                       ^
SyntaxError: invalid syntax
  
So we can use different quotes to achieve what we want.

  >>> print('Alice says "hello" to Bob')
  Alice says "hello" to Bob
  >>> print("Alice says 'hello' to Bob")
  Alice says 'hello' to Bob
  
In case you really want to stick to one quote, either single or double, you need to use add \ (backslash) to espcape

  >>> print("Alice says \"hello\" to Bob")
  Alice says "hello" to Bob
  
Some other special characters such as 
\' or \" include 
  • \' => ' 
  • \" => " 
  • \n => treated as newline 
  • \t => treated as tab
Although \n provides a way to change to new line, it is not so convinient for long text. 

>>> print('this is first line\nthis is second line\nthis is third line')
this is first line
this is second line
this is third line
Thanksfully, Python provides another special method to input long text

>>> print('''this is first line
... this is second line
... this is third line''')
this is first line
this is second line
this is third line

No comments:

Post a Comment