Strings and Lists in Python


Strings can be :

  1. Single quoted   : ‘Hello! I am here’. This is a single quoted string
  2. Double quoted : “Hello! i am here”. This is double “” quoted string.  Anything including escape sequence appear as it is, they are overridden
  3. Escape sequence be used anywhere (\n, \t, \a, \b and so on)
  4. Without print() string appear as it is, but with print () escape sequence have meaning

In Python, single-quoted strings and double-quoted strings are the same.  Pick a rule and stick to it.

When a string contains single or double quote characters, however, use the other one to avoid backslashes in the string. It improves readability.


2.1 String

We can use raw string with ‘r’ in print() function as:


2.2 Strings

Concatenation of string

>>> # three time ram and once syam

>>> 3 * ‘ram’ + ‘Shyam’


2.3 Strings

Strings can be indexed (subscripted), with the first character having index 0. There is no separate character type; a character is simply a string of size one:

2.4 String

If you need a different string, you should create a new one:


>>> ‘J’ + word[1:]


>>> word[:2] + ‘py’


The built-in function len() returns the length of a string:


>>> s = ‘supercalifragilisticexpialidocious’

>>> len(s)                        # this will return the length of string s

Updated: April 19, 2019 — 11:01 am

Leave a Reply

Your email address will not be published. Required fields are marked *

care4you © 2020 Connect On Facebook Protection Status