Skip to main content

Posts

Showing posts from 2022

Split & Join Strings in Python

Split & Join Strings in Python In Python, strings are sequences of characters. Various built-in functions are available to work with strings.  We often come across the need to split the string into a list of substrings (with use of a delimiter character to split the string) and to join a list of strings into a single string. Splitting a String We can use the string method 'split()' to split a string into a list of substrings. This method by default split the string into a list of substrings which are separated by  blank space.  In the below example, we are calling split() method without passing any argument. 1 2 3 4 sample_string = "This is a  string" sub_strings = sample_string.split() print (sub_strings)   Below is the result.  ['This', 'is', 'a', 'string'] If we notice the string, there are two blank spaces between "a" and "string" and same is not present in the substring list. We can also pass the specific de...

OOPs.. Is Java a pure Object Oriented Language?

Object Oriented Programming Object Oriented Programming or OOPs is a programming model which is based on the Objects.  We probably have to talk about the Class before we talk about an Object.  In simple, A class can be considered as a blueprint or template which defines attributes (or variables), members and/or functions containing the behavior of the class. A class doesn't hold or contain any data as is, an instance of the class is to be created to hold and/or process the data.  An Object is an instance of the class, which by default contains the instance of the variables and/or members defined as part of the class.  Any number of objects can be created from a class and each object occupies separate storage and contains different data.  E.g.: Class is like a blueprint of a house. Object is a house constructed physically based on the blueprint. Any numbers of houses can be constructed with the same blueprint.   Is Java a pure Object Oriented Language?...

Mastering String Manipulation in Python: An Introduction to Strings and String Slicing

Strings Strings in python are defined by enclosing with either single quotation marks (") or double quotation marks (") 1 2 str_hello = "Hello..." str_hello = 'Hello...' It does not make a difference in the way string is stored and/or used in the program based on the way it is declared (i.e., by using single quotation marks or double quotation marks).  Multi-line strings Above declaration is applicable when the string is of a single line. If we have a string that has multiple lines, multi-line string can be defined by enclosing with either three single quotation marks (''') and double quotation marks ("""). 1 2 3 4 5 6 7 multi_string = """Hello World, This is a multi-line string. enclosed in double quotation marks"""   multi_string = '''Hello World, This is a multi-line string. enclosed in single quotation marks''' Accessing part of the string by using index String is similar to...