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
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
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)
String as an Array
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
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 indexExample
name="Python" print(name) #printing the 0 index character from string print(name) #printing the 1 index character from string print(name) #printing the 2 index character from string print(name)
P y t
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.
Changing or Deleting a 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) p
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
Looping Through the string
>>> my_name='PythonTheRightWay' >>> my_name = 'i' ... TypeError: 'str' object does not support item assignment >>> my_name = 'Python' #completely reassign >>> my_name 'Python'
Since strings are arrays, we can loop through the characters in a string, with a for loopExample:
string="Python" for char in string: print(char,end= " ") #deault end is a "\n" (new line) # but we override it to a space
P y t h o n
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)
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
There are various methods available with the string object. Some of them are:
Some code examples:
>>>"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'
name = "PythonTheRightWay" print(name[::-1])