Thursday, December 29, 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 delimiter while calling the "split()" method. 

In the below example, we are calling split() method with one blank space " " as an argument and see how the result is different compared to the earlier example. 

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']

We can see an additional entry with no data (this is because of having two blank spaces between "a" and "string". 

split() method considers one blank space as delimiter (as passed) and added an additional entry with no value. 

Let's have a look at another example by passing comma (,) as delimiter. 

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']

There is one other useful argument we could pass to split() method i.e., maxsplit (maximum number of splits). This argument specifies how many splits are to be done on the string. If there are more number of delimiters than the max split passed, it would only split the maximum number of times specified.  

1

2

3

4

sample_string = "This,is,a,string"

sub_strings = sample_string.split(",", 2)

print(sub_strings)

 

Below is the result. 

['This', 'is', 'a,string']

Delimiter is present three times and if we don't use the maximum split, string is split three times (i.e., four elements are returned in a substring. 

In this example, we are passing '2' as max split, so string is split maximum of two times i.e., would return 3 elements with out splitting the string for the 3rd time. 

Joining a List of Strings

Similarly, we often need to join the list of strings. To join a list of strings into a single string, we can use the join() method. 

This method takes a list of strings as an argument and returns a string that is the concatenation of the list elements, with a delimiter character between strings from the list passed. 

Let's have a look at an example. 

1

2

3

4

substrings = ['This', 'is', 'a', 'string']

string = " ".join(substrings)

print(string)

 

Below is the result. 

This is a string

Unlike split() where the method is called from a string and delimiter is passed as an argument, join() method is called from a delimiter and list of strings is passed as an argument. 

And, Removing the blank space as a delimiter would not add any default space or any other character to separate the strings passed. 

1

2

3

4

substrings = ['This', 'is', 'a', 'string']

string = "".join(substrings)

print(string)

 

Below is the result. 

Thisisastring

We can use any other delimiter. Let's have a look at another example by using "-" as a delimiter. 

1

2

3

4

substrings = ['This', 'is', 'a', 'string']

string = "-".join(substrings)

print(string)

 

Below is the result.

This-is-a-string

As we have seen, the split() and join() methods are useful for manipulating strings in Python. They allow you to easily split a string into a list of substrings or join a list of strings into a single string, with the required delimiter. Hope this has been a bit of help in understanding the use of strings in Python.


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

Saturday, November 5, 2022

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? 

We can say Java is a Object Oriented Language, Any functionality that is to be run needs to be defined in a class and corresponding methods/functions. 

There is one thing that we need to consider is the use of primitive data types which are not classes. Java classes/objects can directly access primitive data types making which is deviating from calling it as a Pure Object oriented language. 

There are 8 primitive data types (byte, boolean, char, short, int, float, long and double) in java. 

Saturday, October 29, 2022

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 an array of characters (there is no character data type in Python) and Index starts from 0.

1

2

3

str_hello = "Hello..."

print(str_hello[0]) # H

print(str_hello[1]) # e


Similarly data can be accessed using negative index (i.e., -1 for the last position) 

1

2

3

str_hello = "Hello..."

print(str_hello[-1]) # .

print(str_hello[-4]) # o


Below are the index positions of a string from above example. 

H e l l o . . .

0 1 2 3 4 5 6 7

-8-7-6-5-4-3-2-1

Looping through the string

We mentioned that the string is like an array of characters and we can access the specific character using it's index. 

So, how to loop through all the characters of a string? There are two ways to achieve this.
  • Retrieve string length and loop through the range and access character by using index.
  • Loop through the string directly. 
So, How to calculate the length of the string? 'len()' function returns length of a string. 

1

2

3

string = "Hello World"

for i in range(len(string)):

   print(string[i])


In this example, for loop repeats for every character in the string and character can be accessed by the variable specified (char in this example) through out the loop.

1

2

3

string = "Hello World"

for char in string:

   print(char)


What is string slicing? 

By using the index we can only access one character at a time. We can use slicing to retrieve portion of the string. 

For slicing, we need to provide the starting index (included) and ending index (not included in result). 

1

2

3

4

5

6

7

8

'''

Hello World

012345678910

'''

 

string = "Hello World"

print(string[0:5]) # Hello

print(string[2:5]) # llo


In the above example (Line - 7), we are passing index '0' (as the starting position) and index '5' (as the ending position, not included) which returns the part of the string 'Hello'. Value in the index 5 (' ') is not included in the result. 

In Line - 8, we are passing index '2' and '5', which would return the portion of the string from 2 to 4 (both included).

In the cases like Line - 7 where we are retrieving the porting of the string from beginning till the specified index, we won't really need to include index '0'. 

1

2

string = "Hello World"

print(string[:5]) # Hello


Leaving the start index would consider the string from the beginning. 

Similarly if we leave off end index, it would return the string from start index till end. 

1

2

string = "Hello World"

print(string[6:]) # World


So, what happens if we don't mention both start and end index? It returns the full string. 

1

2

string = "Hello World"

print(string[:]) # Hello World


Check if a character/string present in the string

We have seen how to access a character or a portion of string from a string. Now, Can we check if a character or a string is part of the string or not? Yes, we can do this by using 'in' and 'not in'.

1

2

3

string = "Hello World"

print('H' in string) # True

print('H' not in string) # False


In the above example,
  • Line - 2: We are checking if 'H' is in the string, which returns True if present and False if not present. 
  • Line - 3: We are checking if 'H' is not in the string, which returns False if present and True if not present. 
Similarly, we can use a string to check if it is part of another string. 

1

2

3

string = "Hello World"

print('Hello' in string) # True

print('Hello' not in string) # False


It is highly unlikely we use 'in' and 'not in' operator the way we have described (this is only for better understanding). 

Most likely we would be using this in a condition. 

1

2

3

4

5

string = "Hello World"

if 'Hello' in string:

   print('Hello is present in the string')

else:

   print('Hello is not present in the string')


1

2

3

4

5

string = "Hello World"

if 'Hello' not in string:

   print('Hello is not present in the string')

else:

   print('Hello is present in the string')



We have seen how to define a string & multi-line string, accessing a portion of the string (string slicing) and checking if a character or string is part of the string or not. Hope this has been a bit of help in understanding the use of strings in Python.


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

Different Ways of Sorting Data in a List - Python

Sorting Data in a List List is a collection of data (of different data types), much like an array. Like any data structure or data set, dat...