Python Basic Tutorials

Python is a high level general purpose programming language which has a clear/easy learning curve. Python programming language is massively used in various domains like Artificial Intelligence, Data Science, Web Development, Utilities Tools and Scripts and many more

Python is a Programming language, and the default Python we use is written in C programming language which is also referred to as CPython (Python implementation in C). There are various implementation of Python Programming language i.e Jython(in JAVA), Skulpt(in JS) e.t.c

To make everything easy: We refer CPython as Python

Python Strings

The string is simply the combination of characters. In Python, strings are quoted by either single quotation or double quotation. For example, "PythonTheRightWay" or 'PythonTheRightWay' both are strings.

String Creation in Python:

Strings can be created by writing the value of the string inside the quotation mark


name="PythonTheRightWay" #assigning the string value to name
print(name) #printing the string
Output:
PythonTheRightWay

The string values can also be enclosed within the single triple quotation or in double quotation marks in order to represent a multiline statement or docstring respectively.

    
def string():
    '''This is the docstring part
        inside triple quotation part''' #python docstring.
    pass

print(string.__doc__()) #printing the docstring

a=""" Python is high-level programming languages
    which syntax is extremely very simple and is
    like the English language """ #multiline statement
print(a) 
Output:

This is the docstring part inside triple quotation part
Python is high-level programming languages
which syntax is extremely very simple and is
like the English language
String as an Array

Strings are the combination of character Unicode. Strings are also an array of characters. All the characters are indexed in the string and these can be accessed and modified with the help of the string index

Example

name="Python"
print(name[0]) #printing the 0 index character from string
print(name[1]) #printing the 1 index character from string
print(name[2]) #printing the 2 index character from string
print(name)
Output

P
y
t
String Slicing

You can return a range of characters by using the slice syntax. Specify the start index and the end index, separated by a colon, to return a part of the string.


name="PythonTheRightWay"
>>>print(name[4:]) #slicing from 4th to last character
   onTheRightWay

>>>print(name[:6]) #slicing from 6th to first character
   Python

>>>print(name[6:13]) #slicing from 6th to 13th character
   TheRight

>>>print(name[-1]) #last character
   y

>>>print(name[0])
   p
Changing or Deleting a string

Strings are immutable. This means that elements of a string cannot be changed once they have been assigned. We can simply reassign different strings to the same name


>>> my_name='PythonTheRightWay'
>>> my_name[4] = 'i'
    ...
    TypeError: 'str' object does not support item assignment
>>> my_name = 'Python' #completely reassign
>>> my_name
    'Python'
Looping Through the string

Since strings are arrays, we can loop through the characters in a string, with a for loop

Example:

string="Python"
for char in string:
    print(char,end= " ") #deault end is a "\n" (new line)
                         # but we override it to a space

Output
P y t h o n
String concatenation

Combining two or more strings to make a single string is called string concatenation. We can concatenate 2 or more strings by using operators '+' and '*'

Example

str1="python"
str2="rightway"

concat1=str1+str2 #using "+" operator
concat2=str1*3 #using "*" operator

print(concat1)
print(concat2)
Output

pythontherightway
pythonpythonpython
String Membership Test

We can test if a substring exists within a string or not, using the keyword in


>>>'a' in 'python'
    False
>>>'ontheright' in 'pythontherightway'
    True
String Methods

There are various methods available with the string object. Some of them are:

  • lower()
  • upper()
  • join()
  • split()
  • find()
  • replace()

>>>"PythonTheRightWay".upper()
    PYTHONTHERIGHTWAY

>>>"PythonTheRightWay".lower()
    pythontherightway

>>>"This will split all words into a list".split()
    ['This', 'will', 'split', 'all', 'words', 'into', 'a', 'list']'

>>> ' '.join(['This', 'will', 'join', 'all', 'words', 'into', 'a', 'string'])
    'This will join all words into a string'

>>>'Happy New Year'.find('ew')
    7

>>>'Happy New Year'.replace('Happy','Brilliant')
    'Brilliant New Year'
Some code examples:


name = "PythonTheRightWay"
print(name[::-1])
Output:
yaWthgiRehTnohtyP