Friday, February 26, 2021

Python - Index and strings

 

Index and strings



Function: around 50 function be comfortable
built-in functions
len() 
type()
id()
print()
input()
Find length of string
str = 'Core Python'
print(len(str1)
you get output: 11
space is also counted.
This is print the total length of a string.
Open idle
+ -> 
* ->
==
<
>
>>> s1="Core"
>>> s2="Python"
>>> s3=s1+s2
>>> print(s3)
CorePython
>>> s3= s1 + s2
>>> print(s3)
CorePython
>>> s1="Python "
>>> s2="Core"
>>> print(s3)
CorePython
>>> s3=s2+s1
>>> print(s3)
CorePython 
>>> s3=s1+s2
>>> print(s3)
Python Core
>>> 
>>> s="Python"
>>> t=s*3
>>> print(t)
PythonPythonPython
>>> str1="Python"
>>> str2="python"
>>> print(str1==str2)   
False
>>> 
uppercase comes before lower case
>>> print(str1<str2) 
True
Its going to compare in dictionary order. Capital letters come before small letters
>>> user1="guest"  
>>> user2="gUest"   
>>> print(user1<user2)   
False
Slice a string
-----------------
Slice -> a piece or a part of a string.
slice has a syntax
stringname[contains Three part]
stringname[start:stop:step]
you have stringname square braces, followed by start, and stop and then step
step default value is 1.
you will get value start to stop-1 value
lets say start is 0 and stop is 9
so you will get from index 0 to 8
how do we get part of string? - slicing
For eg,
str1='Core Python'
We have our string as
C o r e   P y t h o n
0 1 2 3 4 5 6 7 8 9 10
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1  -> negative index
print(str1[0:4]) => what output you expect from this 
we start from 0 and go upto 4 so we have 4-1=3
so we will get 0 to 3 => Core
print(str1[0:9:2]) what's the output?  => Cr yh
start index is 0
gives output to 0 to 8
but the skip is 2 characters
so you get in index
0 2 4 6 8 (increment by 2)
so the output is Cr yh
print(str1[::] -> what's the output?
we didn't see start, stop and step.
by default value of step size is 1.
Its going to be Core Python
print(str1[:4:]) -> what's the output?




Most of the data in string
string manipulation next class

No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...