x

Python Strings

PREV    NEXT

  1. String is nothing but collection of sequence of character,whichever enclosed in single quotes(‘ ‘),double quotes (“ “) and triple quotes (”’ ”’)
  2. Python strings are immutable
  3. String can be a cobination of numbers and alphabates
  4. String can be a empty also ,that has 0

Example:

Output:

Create empty string:

str1=”


str2=””

str3=”””

print (str1)

print (str2)

print (str3)

Output:

Note: it will print blanck on command prompt

Escape Sequence:

We can use escape sequence in string for special cases

Examples:

Escape Sequence Meaning of ES
\n Newline – It will Prints a newline character
\t Tab – It will Prints a tab character
\\ Backslash – It will Prints a backslash ( \ ) character
\’ Single quote – It will Prints a single quote
\” Double quote – It Will Prints a double quote

Example:

Output:

Access the individual characters in strings:

To Access the individual characters from string we can do it in two ways.They are

1.Forward Indexing

2.Backward Indexing

1.Forward Indexing:

P Y T H O N

Index                           0                      1          2                      3                      4                      5

As per the defination of string it will stored in sequence .We can access the individual characters in string by using the string variable and index.As per above schematic we need to generalize that forward index will start from “0” th index and end with “n-1” index.To use the index we can use square brackets “[]”

Below is the example to access the charatcters in the string

course=”python”

course[0]=’p’

course[1]=’y’

course[2]=’t’

course[3]=’h’

course[4]=’o’

course[5]=’n’

Below is the example is showing how to iterate the each character by using loops


Example:

Output:

2.Backward Indexing or negative indexing:

We can also use negative index in string to access the individual characters.Negative index will start from the last character ,it will start with “-1” and end with “-n”

P Y T H O N

Negative Index           -6                     -5         -4                     -3                     -2                     -1

Below is the example to access the charatcters in the string

course=”python”

course[-6]=’p’

course[-5]=’y’

course[-4]=’t’

course[-3]=’h’

course[-2]=’o’

course[-1]=’n’

Example:

Output:

String Slicing:

String slicing is used to get the set of characters at a time.To do this we can use the slicing operator(:)

Syntax:

string_name[startindex:endindex]

  • Return:
    It will return the slice of characters from the start index to end index,the character end index is not be include in the slice.
  • If endindex is  greater than the string  length then it will give the complete string from start index to complete stringNote: Here index is optional

Example:

Output:

Example:

Output:

String Methods In Python:

By using string methods we can find the sub string in main string,convert string etc.

Syntax:

string_name.method_name(arg1,arg2…..argn)

Note:Here arguments are optinal

Methods for  find and replace the strings:

Example:

Output:

Strings converting Methods:

 Example:

Output:

Strings For Testcase:

 Example:

Output:

String Formats:

 Example:

Output:

PREV    NEXT



Like it? Please Spread the word!