Python Programming - Class #3 - CS Expert

Latest

Random Posts

Seo Services

Monday, November 12, 2018

Python Programming - Class #3


Strings

Concatenation:-
Concatenation is the process of Combining two strings.
We use “+” symbol to concatenate two strings.
For Example:-
think = "Think"
python = 'Python'
think_python = think + ' ' + python
print(think_python)

String Multiplication:-
Python Language support the Property of String Multiplication.We can multiply the the String.
For Example:-
love = "love"
fifteen_of_love = love * 10
print(fifteen_of_love)

String Indexing:-
We can access any character of string by using index number of that character.
For Example:-
python = "Python"
print("o " + python[4])     # Note: string indexing starts with 0
p_letter = python[0]
print(p_letter) 

String Negative Indexing:-
We can also use the negative indexing to access the Desired character by counting “Backword” the string.
For Example:-
long_string = "This is a very long string!"
g_word = long_string[-2]
print(g_word)

String Slicing:-
String Slicing is used to convert a long string into Sub Strings.We can divide a long string into Sub Strings.We can also divide more then one character by using Slicing.
str[start:end] # items start through end-1
str[start:]    # items start through the rest of the array
str[:end]      # items from the beginning through end-1
str[:]         # a copy of the whole array
For Example:-
think_python = "Think Python"
think = think_python[:5]      # one or both index could be dropped. think_python[:5] is equal to monty_python[0:5]
print(think)
python = think_python[6:]
print(python)

In Operator:-
We can search any character by using “in operator” in a string.
For Example:-
think_python = "think python"
print("think" in think_python)    # print boolean result directly
contains = 'python' in think_python
print(contains)

String Length:-
We can use the “len()” function to calculate the length of a string.
For Example:-
pakistan = "I love Pakistan"
print(len(pakistan))

Basic String Method:-
There are a lot of methods in Python Programming.We can use lowers() and Uppers() function to write the String in Lower case or Upper Case..
For Example:-
love_python = "I Love Python"
print(love_python)
print(love_python.lower())    # print lower-cased version of the string
print(love_python.upper())

1 comment: