String Operations in Python. |print all letters appear in two words|

String contains the slicing operator and the slicing with the stepsize parameter is used to obtain the subset of string. It also has basic concatination " + " in and repeatation " * ".

String Slicing Operators

The slicing operator return a subset of string called slice by specifying two indexes start and end.

Syntax: Var_name[start:end]

Example:

st="python string" print(st[4:10])

output:

on str

String Slicing with step size

If the programmer select every second character from string. This can be done by using step-size. We need to add third parameter as step-size.

Syntax: Var_name[start:end:stepsize]

Example:

st="python basic string" print(st[1:10:2])

output:

yhnbs

Some more Complex examples:

st="python basic string" print(st[::]) print(st[::-1]) print(st[-1::-1]) print(st[:-1])

output:

python basic string gnirts cisab nohtyp gnirts cisab nohtyp python basic strin

The Plus(+) operators

The + operator is concatination operation is used to join to string.

Example:

st="python" st1="Language" print(st+st1)

output:

pythonLanguage

The Repeatation operators(*)

The multiplication (*) operator is used to concatinate the same string multiple times. It is also called as Repeatation operators.

Example:

st="python" print(st*3)

output:

pythonpythonpython

In or Not In operator

Both operator "in" and "not in" are used to check weather a string or substring present in another string.

Example:

st="python language" print('language' in st) print('basic' not in st)

output:

True True

Write a program to print all the letters from word1 also appear in word2.

st=input("Enter word1 ") st1=input("Enter word2 ") for str in st: if str in st1: print(str)

output:

Enter word1 python basic Enter word2 python p y t h o n

Comments

Popular posts from this blog

Introduction to Python.

Decision Making Statement.