Sunday, September 16, 2018

Python Ninja bootcamp 5-Strings

Learn Python like a Professional! Take You from 0 to Hero



In this Python lecture, we will learn about the Strings.


"String is set of sequence of characters represented in quotation marks".
                               Or
"String is ordered a collection of immutable elements".

String is either define under single quote or double quote.



Let's look at String example-

Create Strings


# Single word
'hello'
>> 'hello'


# Entire Sentence
'Welcome to Python Ninja Bootcamp'
>> ' Welcome to Python Ninja Bootcamp '

We can also use double quotes 
" Welcome to Python Ninja Bootcamp "
>> ' Welcome to Python Ninja Bootcamp '


#Be careful with quotes
'I'm Mayank'
>> SyntaxError: invalid syntax
We are getting syntax error because single quotes is close after is 'I'. Instead of that we can use combination of single quotes and double quotes

" I'm Mayank "

>> " I'm Mayank "

We can also put a double quote inside a single quote
'" Life is beautiful "'
>> '" Life is beautiful "'


'Hello World1'
'Hello World2'
>> 'Hello World2'

We can use print function to print both the strings
print('Hello World1')
print('Hello World2')
>> Hello World1
>> Hello World2

# \n Breaks the line
print('This is the first.\n And this Second line.')
>> This is the first.
      And this is Second line.

# \t Creates the spaces
print('This is the first.\t And this Second line.')
>>  This is the first .       And this is Second line.

# len() Calculates the length of string  
len('hello world')
>>  11

Now assign a String to a variable
x = 'hello world'
print(x)
>> hello world


String Indexing


We have x is equal to 'hello world'

x[0] # garbs the letter at index 0
>> h

x[2] # garbs the letter at index 2
>> l

x[-1] # garbs the last letter
>> d


String Slicing


We can use : (colon) to perform slicing

x[1:] # garbs everything from the first index 
>> ello world


x[:] # garbs everything
>> hello world


x[:3] # garbs everything from first index 0 to third index(excluded)
>> hel

x[:-1] # garbs everything from 0 index to the last index(excluded)
>> hello worl


x[1:5:2] # garbs every 2nd element from the 1st index to 5th index(excluded) 
>> el

*Important Syntax
x[::-1] #  Reverse the string
>> dlrow olleh

x[1:5:2] # garbs every 2nd element from the 1st index to 5th index(excluded) 
>> el


String Properties


Immutability

Once String is created, the elements within it cannot be changed or replaced.

We have x is equal to hello world
x[0]='a'
>> TypeError: 'str' object does not support item assignment

We can concatenate the string.
x + ' concatenate '
>> hello world concatenate


Built-in Function


We have x is equal to hello world
x.upper() # Convert the string to uppercase
>> HELLO WORLD

Note: If press TAB after x. in Juypter notebook. It will show the list of functions available in the string.

x.lower() # Convert the string to lowercase
>> hello world

x.split() # Split the string at Space by default
>>[' hello',' world' ]

x.split('o') # Split the string at 'o'
>> [ ' hell ',' w ', ' rld ' ]

Practice other built-in functions.



EDITS ARE WELCOMED!!

In the next Blog, we will discuss Print formatting  

https://sngurukuls247.blogspot.com/2018/09/python-ninja-bootcamp-6-print-formatting.html

......................................................................................................................................

Follow the link below to access Free Python Lectures-
https://www.youtube.com/sngurukul


View the Jupyter Notebook for this lecture

Download the Jupyter Notebook for this lecture 



Instagram

Feel free contact me on-
Email - sn.gurukul24.7uk@gmail.com

No comments:

Post a Comment