EMAIL SUPPORT

dclessons@dclessons.com

LOCATION

US

Strings & its Operations

Strings & its Operations

Strings Overview

Stings are closed collection of Unicode Characters. These string can be enclosed in single, double, or triple quotes.

Example: ‘dclessons’, “dclessons”, ‘’’dclessons’’’, “””dclessons”””

If you have character like ‘ “or \ with in string they can be retained either in two ways.

  • Escape it by preceding them with a \
  • Prepend the string with ‘r’, showing them this is raw string.

X = ‘Dclessons is, \ ‘very good online portal.\’’

X = r‘Dclessons is ,\ ‘very good online portal.’’

Multistring can be created in three ways.

  • Write the string but put \ at end of each line
  • Enclosed with in “””” XXXXXX””” or ‘’’ XXXXX’’’
  • (‘first msg’ ‘Second string’)

Example 1: Let’s understand how to create Simple and Multi-Line Strings. 

String1 = 'Dclessons'                                                                                                                                                print(String1)                                                                                                                                                            String2 = 'Dclessons is \'top online webportal'                                                                                                    print(String2)                                                                                                                                                          String3 = 'Dclessons is top online training ...\ webportal in DataCenter technology'                                                print(String3)                                                                                                                                                            String4 = """Dclessons is top online training webportal in DataCenter Technology"""                                              Print(String4)                                                                                                                                                            ==============================                                                                                                        Result                                                                                                                                                                      Dclessons                                                                                                                                                                Dclessons is 'top online webportal                                                                                                                          Dclessons is top online training ... webportal in DataCenter technology                                                                Dclessons is top online training                                                                                                                                                  webportal in DataCenter Technology

Example 2: How to do String Replication.

String1 = 'Dclessons'                                                                                                                                                print(String1*5)                                                                                                                                                        String2 = 'dclessons is'                                                                                                                                            String3 = 'Good Online Portal'                                                                                                                                  String4 = String2+String3                                                                                                                    print(String4)                                                                                                                                                            print(len(String4))                                                                                                                    =========================                                                                                                                          Result                                                                                                                                                                    DclessonsDclessonsDclessonsDclessonsDclessons                                                                                              dclessons isGood Online Portal                                                                                                                              30

String Numbering

Strings can be accessed via index Value. It starts with 0 Index Value. Negative Index Value is also allowed. Let’s see below example how String are indexed.

0

1

2

3

4

5

6

7

8

D

C

L

E

S

S

O

N

S

-9

-8

-7

-6

-5

-4

-3

-2

-1

Example

X = DCLESSONS

  • Y = X[2] = L
  • Y=X[4] = s
  • Y = X[-6] = E

Now if you want to slice out some sub-string from main strings , it can be done by using following string functions.

  • X[start : End] : It extract from start to End -1
  • X[Start:]: it Extract from start to End
  • X[:End] : It extract from start to End -1
  • X[-Start :] : It extract from –Start ( including ) to End

Example 3: Lets for String X = Dclessons, write a Program to get following output.

  • Dc
  • Dc
  • lesson
  • lessons
  • snosselcD


Comment

    You are will be the first.

LEAVE A COMMENT

Please login here to comment.