How to join strings separated by a dot in Python

When one codes computer tools and software in their favorite computer programming language, they deal with a lot of frustration, especially in the beginning when they don’t know what they’re really doing. Based on my personal experience with the Python computer programming language, there are many different ways to accomplish a certain task with Python, but only a few of them are accepted as legit and the norm by those who get paid to write professional code.

Not that there is completely wrong to accomplish your tasks in Python computer programming language by going your own way, but it is always a good idea to stay updated with the latest tips from the professional software developers as they get paid to solve coding problems, and they know for sure what they do.

Right now, I am writing a project in Python computer programming language and there is a problem which has occurred to me. From my personal experience with the Python computer programming language, I believe I do know two ways to solve my problem from a coding perspective.

So what is the problem that has occurred to me and that needs to be solved? I need to join two strings separated by a dot like shown below.

test # first string
mp4 # second string
test.mp4 # final string

According to a book which I am studying, related to the Python computer programming language, one can easily perform string concatenation by using the addition arithmetic operation like shown in the following example.

‘test’ + ‘.’ + ‘mp4’

Although the above technique works when it comes to joining strings together in Python computer programming language, the professional software developers recommend another method to accomplish the same task. According to the official Python documentation, string objects support a specific method called join, which takes a list of strings and joins them together.

One can easily use the string specific method called join like shown below.

‘,’.join([‘a’, ‘b’])

When the above piece of Python code is been executed in the interactive console, the following output is being displayed.

‘a,b’

Based on the above technique, one can easily concatenate two strings together separated by a dot like shown in the following example. The syntax is very clear and easy for anyone to understand.

‘.’.join([‘test’, ‘mp4’])

Once the above Python operation is executed on my interactive console, the following comes up.

‘test.mp4’

As you can see from the above output, one can make use of the specific string method called join, to concatenate together a list of things.

Final thoughts

As I mentioned earlier in this article, one can accomplish a certain task in many different ways when it comes to the Python computer programming language, but there are only a few ways accepted and used by the professionals. The .join method is perfect when one needs to join strings together, especially separated by a dot.

How to clear the console with Python

When one writes and practices a lot of code in their Python interactive shell, their console gets dirty, so it is necessary to clean it often. There are many ways to accomplish a task with the Python computer programming language, like with any computer technology out there, but in here I will explain only a few techniques to help the Python beginner to remove the dirt from their Python interactive console.

Having my Python interactive shell full of code, dirty to the point that it is necessary for me to clean it, first of all, I will import a module which helps to execute system commands. The name of the module is os, it can be used by the Python coder to run different shell commands part of the operating system. Not only that, the Python os library offers other builtin functionalities related to the operating system.

First, use the Python import statement like shown below, with the main purpose of importing the utilities offered by the Python os package.

import os

For one to execute a shell command part of the operating system with the help of the os module, they have to follow the syntax which is being shown below.

os.system(cmd)

The syntax for the os module being shown above, is very clear to anyone who has a little knowledge on the Python computer programming language. Everything one needs to execute a shell command on their operating system is the utility shown above, and the command.

For the purpose of this short tutorial, I am going to explain to the Python beginners how they can make use of the utilities being shown below to clear their interactive consoles.

os.system(‘cls’) # for windows users
os.system(‘clear’) # for unix users

Final thoughts

The os module is very useful to the Python developer. There are many utilities that come with it, but the one you need to run shell commands specific to the operating system you’re working with, is the os.system tool.

Python lists explained in details for complete beginners

Having coded with Python computer programming language for quiet a long time now, I am completely aware of the importance of list object, especially when it comes to writing software that processes information. Through this article, I am going to explain the Python list object in details to the complete beginner.

A list in Python computer programming language is an object which is mainly being used to store other kind of objects such as strings, integers, floating points, tuples, dictionaries and for sure other lists. The syntax for declaring a list object in Python is very simple, completely easy to read and understand.

One can easily write a Python list by following the syntax shown below.

l = []

As you can see from the above example, the syntax for a list object in Python computer programming language looks like a package in which other Python objects are being packed.

The following is another Python list object with some elements.

l = [1, ‘a’, (), {}, []]

Like many other objects in Python computer programming language, a list object has a length which one can easily find out by using the builtin len function like shown below.

len(l)

Once the above code is being executed successfully on the Python interactive shell, one will get the following output displayed on their console.

5

The Python coder can easily access the elements of a list object by using the operating of indexing like shown in the example below.

first_el = l[0]

The indexing starts from 0. So the first element in the above list has an index of 0, the second element has an index of 1 and so on.

To get our hands dirty and practice the concepts of lists in the Python interactive shell, try to access the second element of the list we have created by using the syntax which is shown below.

second_el = l[1]

One can easily check the value of a list element by using a conditional statement like it is being shown in the following example.

l[0] == 1

The value shown below gets displayed on the console once the above piece of Python code is executed in the Python interactive shell.

True

Being mutable objects, Python lists can be easily updated. The coder can change an element of a list by using the following syntax.

l[0] = 2

If the above piece of Python code is being executed successfully, the list will not remain the same. The above operation, gets the first element of the list named l, and replaces it with another element, in our case the element is the number 2.

To check if the list is updated or not, just type the following piece of Python code and see the result being displayed on the interactive shell for yourself.

l

Once the above piece of Python code is being executed on my Python console, the following is being displayed in the interactive shell.

[2, ‘a’, (), {}, []]

As you can see from the above output which is displayed on the Python interactive shell, the list has been updated, since the first element with an index of 0, does not equal anymore to the value of 1 like it did in the beginning when we created the list.

A simple check can prove it.

l[0] == 1

False

l[0] == 2

True

Python list specific operations

Like most of the Python objects, lists support specific operations which do work only for them. One can easily add a new a element to their Python list by using the specific method named append like shown in the following example.

l.append(‘new’)

A simple check to see if the new element has been added in the list or not can be done like shown in the example below.

l

The following comes up on the Python interactive console after the above piece of code is being executed.

[2, ‘a’, (), {}, [], ‘new’]

From the result being shown above, one can easily understand that the specific list method named append serves to update the list with new elements.

How about removing an element from the list, is there any specific method for doing it? Yes, the pop method can be used to remove a specific element from a list in Python computer programming language.

The Python syntax for the pop method, specific to list objects, is being shown below.

l.pop(0)

The above Python command, is being used to remove the element with an index of 0 from the list which is assigned to the variable l.

According to the official documentation, the specific list method named pop, takes as an input an argument, the index of the element which the user wants to remove from their list.

To check if the element with an index of 0, the first one in the list, is being removed or not, type the following command in your own Python interactive shell.

l

Once the above command is being executed in my Python interactive shell, the following list is being displayed on the console.

[‘a’, (), {}, [], ‘new’]

As you can see from the above result, the specific list method pop, is the perfect utility when it comes to removing an element from a list.

Let’s create a new list this time with the main purpose of practicing the specific methods which belongs to list objects, already builtin in Python.

t = [‘a’, ‘c’, ‘b’]

One can easily print the above list in the Python interactive console by using the already builtin Python print statement like shown below.

print(t)

The above command produces the following output on the Python interactive shell.

[‘a’, ‘c’, ‘b’]

A method which can help the Python coder to present the above list in the interactive console as part of the english alphabet, ordered according to the english alphabet, is the method called sort. It’s practical syntax and usage is being shown below.

t.sort()

To check the ordered list, just print is by using the Python builtin print statement like shown below.

print(t)

Once the above print statement is being executed in the Python interactive shell, the following gets displayed on the console.

[‘a’, ‘b’, ‘c’]

As you can see from the above output, displayed on the interactive Python console, the specific list method called sort, can be easily used to sort out the elements of a list object.

Create a new list with numbers as elements like shown below.

n = [0, 3, 2, 1, 4, 5, 6, 7]

Then try to sort the above list with the specific list method we explained.

n.sort()

After the list is sorted, try to print it like show below.

print(n)

Once the above Python statement is being executed, the following gets displayed on the console.

[0, 1, 2, 3, 4, 5, 6, 7]

As you can see from the result which is shown above, the sort method sorted the numbers in the ascending order. How about sorting the elements of a list in the descending order, is there any builtin utility in Python computer programming language for achieving that?

Yes, there is. The syntax for it is being shown below.

n.sort(reverse=True)

The above argument, reverse=True, tells Python to sort the list in the opposite order; in this case in the descending order.

print(n)
[7, 6, 5, 4, 3, 2, 1, 0]

Final Thoughts

List objects in Python computer programming language serve a great purpose when it comes to building data structures. Rich in features, they support specific operations which can help the coder to perform actions such as sort the elements of the list, insert new elements in the list, pop out elements from the list, copy the list and many others which I am going to explain in details in another article.

How to update a list in Python 2.7

Python lists help the coder to store information for later usage in their software development projects. As you may know, one can easily declare a list object in Python computer programming language by using the syntax which is being shown below.

list_object = []

Based on my personal experience with Python computer programming language, a list can have different type of elements in it. To make things more practical, I am writing the following list.

list_object = [1, 2, 3, ‘coder’]

Being mutable objects, Python lists can change in time. What does it mean? It means that once you have declared a list, you can easily update its elements by using the builtin operations supported by the list object.

The following syntax can be used in Python code to append a new element in a list.

[].append(element)

If one tries to run the above code, their Python interactive shell will throw an error, because the element has not been declared. The example shown above stands only for explaining the append operation, which is specific to list objects in Python computer programming language.

Now, let’s get our hands dirty. Having declared a real list in our Python interactive shell, we can easily update it, by using the append operation; like shown below.

list_object.append(4)

Once the list is updated successfully, we can easily check its new element like shown below.

list_object

The following will get printed on your Python console.

[1, 2, 3, ‘coder’, 4]

Not only do list objects support population with new elements, but they also support depopulation too. The syntax for removing an element from a list is being shown below.

[].pop(index_of_element_to_be_removed)

Let’s try to remove from the list, the element we just added by using the pop operation like shown in the following example.

list_object.pop(4)

If the above piece of Python code is being executed successfully in your Python interpreter shell, you will get the following output on the console.

4

In our example, the index of the element we removed, is the same as the value of the element.

To remove another element from the list_object, use the pop method of the Python list, by providing as an argument the index of the element you want to remove.

Let’s try to remove the element ‘coder’ from the list_object.

list_object.pop(3)

If the above piece of Python code is being executed successfully in your Python interactive shell, you will get the following output.

‘coder’

And now, if we check the list named list_object, we will realize that it has been updated.

list_object

Once the above code is being run, the following comes in the Python console.

[1, 2, 3]

Final thoughts

There is another way which I do know that can be used to update list objects in Python, but I am going to share that in another article, in which I am going to explain Python lists in details for complete beginners. Specific Python list methods such as append and pop, are enough to add new elements to the list, and remove existing elements from the list.

How to use the def statement in Python to create a function

Functions play a very important role in computer programming, especially when it comes to grouping code statements together with the main purpose of creating specific utilities to solve certain problems. Being a feature rich computer programming language, Python comes with the tools which one can easily use to write functions to automate their daily tasks.

The following syntax is being used to create a function in the Python computer programming language.

def find_sum():pass

A function in Python is composed of nested statements. It acts like a package of the statements so it can group them together to solve a specific problem. For a function in Python computer programming language to properly work, it must return something back to the caller.

A coder can use the Python return statement inside a function to return something to the caller. The syntax for it is being shown below.

def find_sum():return 1

If executed, the above function will return the value of 1 back to the caller. To make things more practical, let’s fire up the Python interactive shell and run the code being shown above.

find_sum()

After running the above, in my Python interpreter shell, the following value was returned.

1

As a Python coder, you can use the def statement to write your own custom functions. The above one, does not do anything special at the moment, it just returns a value.

A real world function in Python computer programming language, should make use of arguments. The syntax for it is being shown below.

def find_sum(a, b):return a + b

The above function takes two arguments, a and b. Then it calculates and returns their sum. To run the above function, you have to provide two numbers like shown below.

find_sum(5, 3)

Once it is being run, the above code returns the following result.

8

Final thoughts

To code in a computer programming language, means to solve specific problems. Python functions play a very important role in software development. One can easily create their own custom functions by using the def statement like explained earlier in this article.

How to check if a specific path exists or not by using Python

Each file or folder in the filesystem has a path, a specific one. Python coders deal a lot with paths of files in their computer programming projects, especially when developing software that deals with the utilities of the operating system. Being rich in already builtin features, Python offers many utilities that can help the coder accomplish the tasks they want, without any pain.

One can easily use the os module offered by Python to check if a specific path exists on their operating system or not. The following syntax, shows the usage of the os.path.exists utility.

os.path.exists(path_in_here)

When the os.path.exists Python statement is executed, it returns True or False, depending on the existence of the path which is being checked. It’s usage is very easy, but before one can do that, they have to import the os module first, like shown below.

import os

Once the Python os module is imported, one can easily utilize its resources. Since the purpose of this tutorial is to teach one how to check for the existence of the path of a file in their operating system, I am giving the following practical example to illustrate it.

import os
os.path.exists(‘test’)

When the above statements are executed in the interactive Python shell, False is being returned.

False

Why? Because the path which we are looking for does not exist in the system. With the main purpose of illustrating the usage of the os.path.exists utility when a path exists, I am running the example shown below on my unix based machine.

os.path.exists(‘/usr/local/bin/python’)

When the above Python statement is executed on the Python interactive shell, on my computer, the True value is being returned.

True

This time the True value is being returned, because the path which I am checking for, exists in the operating system.

Final thoughts

Python comes with the builtin tools that can help the coder solve almost any kind of problem. By using the os.path.exists utility, one can check for the existence of a path in their operating system.

How to find the length of an object in Python

Python is a high level computer programming language which comes with many already builtin tools that help the coder to accomplish almost any task, they need to. Being an object oriented computer programming language, some of the Python objects have a length, which can be easily accessed with the help of the Python builtin utility called len().

Python objects that have a length are strings, tuples, lists and dictionaries. One can easily use the Python builtin function len() to find the length of an object which supports one.

According to the official Python documentation, the Python builtin function len(), stands for the length of an object in Python computer programming language. As I have mentioned in earlier tutorials related to Python, the Python computer programming language is easy to read, like a human language.

The following is the syntax which can be used to utilize the Python builtin function len(), with the main purpose of finding the length of an object.

len(Python object which supports length in here)

With the main purpose of illustrating the usage of the Python builtin len() utility in a practical example, I am creating the following string.

website = ‘codetheory.in’

To find the length of the above Python string, the Python builtin len() utility can be easily utilized like shown below.

len(website)

When the above piece of code is being run in the Python shell, in interactive mode, the following is being displayed on the console. It is the number of the characters found in the string ‘codetheory.in’.

13

As mentioned earlier in this article, Python objects such as tuples, lists, and dictionaries have a length too. To give other practical examples of the Python len() utility in action, I am building the following objects.

t = (1, 2, 3)
l = [1, 2, 3, 4, 5]
d = {‘profession’: ‘coder’}

In the above Python code, there are three objects being declared: a tuple, a list and a dictionary. One can easily find the length of each one of the above Python objects by using the builtin len() utility in Python computer programming language; like shown below.

length_of_tuple = len(t)
length_of_list = len(l)
length_of_dictionary = len(d)

To print the length of each of the Python objects declared about, make use of the Python print statement like shown below.

print(length_of_tuple)
print(length_of_list)
print(length_of_dictionary)

Final thoughts

The Python computer programming language comes with many already builtin tools which can ease the work of the coder and speed up the process of development. One of them is the len() function, which can be used to find the length of a Python object which supports one.