Posts

Showing posts with the label string

String Formatting in Python

Image
  Concept In Python below code will create an 'Type Error' because python does not support this kind of operation. age = 36 txt = "My name is John, I am " + age print(txt)  Output: TypeError: must be str, not int So get rid of this issue we basically do ' String Formatting ' by using format() method.   Code Example 1: txt = "I love {} & {}" print(txt.format('Python', 'Django')) Output: I love Python & Django   Example 2: txt = "I love {0} & {1}" print(txt.format('Python', 'Django')) Output: I love Python & Django   Example 3: txt = "I love {1} & {0}" print(txt.format('Python', 'Django')) Output: I love Django & Python   Example 4: txt = "I love {x} & {y}" print(txt.format(x='Python', y='Django')) Output: I love Python & Django Get Help

Python String Data Type

Image
  Example String is one the datatype for python. String literals in python are surrounded by either single quotation marks, or double quotation marks. 'hello'  is the same as  "hello". Python Support positive as well as Negative Indexing var = "Hello" is same as var = 'Hello' var = "I Love Python" # Multiline String var = ''' I Love Python I Love Django '''' ''' ''' ---> this one is known as docstring in python This is How Python store a string, see the below Image a =  "Hello, World!" print (a[ 0 ]) Output: H a =  "Hello, World!" print (a[1 ]) Output: e a =  "Hello, World!" print (a[- 1 ]) Output: ! a =  "Hello, World!" print (a[ -2 ]) Output: d # Python Slice Concept # Remember it touches Lower bound not Does not touch the upper bound. a =  "Hello, World!" print (a[ 0: 3 ]) Output: Hel a =  "Hello, World!" print (a[ 2: 3 ]) O...