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".
"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'
# Entire Sentence
'Welcome to Python Ninja Bootcamp'
We can also use double quotes
" 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 "
We can also put a double quote inside a single quote
'" Life is beautiful "'
Print a string
'Hello World1'
'Hello World2'
We can use print function to print both the strings
print('Hello World1')
print('Hello World2')
>> Hello World2
# \n Breaks the line
print('This is the first.\n And this Second line.')
And this is Second line.
# \t Creates the spaces
print('This is the first.\t And this Second line.')
# len() Calculates the length of string
len('hello world')
Now assign a String to a variable
x = 'hello world'
print(x)
String Indexing
We have x is equal to 'hello world'
x[0] # garbs the letter at index 0
x[2] # garbs the letter at index 2
x[-1] # garbs the last letter
String Slicing
We can use : (colon) to perform slicing
x[1:] # garbs everything from the first index
x[:] # garbs everything
x[:3] # garbs everything from first index 0 to third index(excluded)
x[:-1] # garbs everything from 0 index to the last index(excluded)
x[1:5:2] # garbs every 2nd element from the 1st index to 5th index(excluded)
*Important Syntax
x[::-1] # Reverse the string
x[1:5:2] # garbs every 2nd element from the 1st index to 5th index(excluded)
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'
We can concatenate the string.
x + ' concatenate '
Built-in Function
x.upper() # Convert the string to uppercase
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
x.split() # Split the string at Space by default
x.split('o') # Split the string at 'o'
Practice other built-in functions.
In the next Blog, we will discuss Print formatting
https://sngurukuls247.blogspot.com/2018/09/python-ninja-bootcamp-6-print-formatting.html
......................................................................................................................................
View the Jupyter Notebook for this lecture
Download the Jupyter Notebook for this lecture
Instagram
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/sngurukulView the Jupyter Notebook for this lecture
Feel free contact me on-
Email - sn.gurukul24.7uk@gmail.com
No comments:
Post a Comment