Python Category Page - PythonForBeginners.com https://www.pythonforbeginners.com Learn By Example Fri, 30 Jun 2023 13:02:40 +0000 en-US hourly 1 https://wordpress.org/?v=5.8.13 https://www.pythonforbeginners.com/wp-content/uploads/2020/05/cropped-pfb_icon-32x32.png Python Category Page - PythonForBeginners.com https://www.pythonforbeginners.com 32 32 201782279 Hello Developers. Meet Python. The King of Growth https://www.pythonforbeginners.com/basics/hello-developers-meet-python-king-growth Wed, 03 Jan 2018 17:04:01 +0000 https://www.pythonforbeginners.com/?p=6753 For the most part, it’s difficult to crown just one language as the supreme leader of standard use in the development world. There isn’t a language that takes the cake, though there are developers that certainly prefer one or more over the others. Growth is not surprising to see anymore either, especially in today’s highly […]

The post Hello Developers. Meet Python. The King of Growth appeared first on PythonForBeginners.com.

]]>
For the most part, it’s difficult to crown just one language as the supreme leader of standard use in the development world. There isn’t a language that takes the cake, though there are developers that certainly prefer one or more over the others. Growth is not surprising to see anymore either, especially in today’s highly virtual and digitized world. Technologies like AI, automation, machine learning, mobile and web design all favor development which in turn feeds the demand for more programmers and development professionals.

That said, if there’s only one language you select to learn due to its incredible potential let it be Python. Why? We’re going to take a closer look at the incredible rise Python is seeing right now, especially in high-income and development-heavy countries.

Python, at least right now, can be considered the king of growth in the language and development space. Lofty claim? Don’t believe it? No problem, let’s dive in and find out why we’re saying such a thing.

What Are “High-Income” Countries?

First, we need to define “high-income” countries and why that’s an important distinction to make when looking at current trends and statistics.

High-income countries include those where it’s fairly common to earn a wage or salary that meets the industry average; usually this falls somewhere between $60,000 to $140,000, but depends on the country in question.

The most common countries included in this demographic are United States, United Kingdom, Germany, and Canada. Keep in mind, these are not the only major contributors in the global software development industry. Just as impactful are Russia, China, Brazil, and many others – though they certainly lack in salary and wage offerings.

And while Python is showing growth in all these markets, it’s the high-income markets that are showing the most influential numbers.

How the Growth Was Measured

As reported by David Robinson on the Stack Overflow blog, you can use the platform’s Trends tool to view and measure growth of various development languages, for which Python is no exception.

Out of five major languages – also CSS, HTML, Android, and JQuery – Python is seeing incredible growth in high-income countries, going all the way back to 2011. In June 2017, Python became the most visited tag on Stack Overflow, among high-income countries. That’s an impressive sight, compared to 2012 when it was the least visited tag of the five languages. In just five years, popularity for the language has more than doubled.

Robinson does go on to explain some underlying reasons why this change happened. Java has seasonal traffic thanks to educational purposes, for example. Still, there’s no denying the growth of Python.

He’s not the only one to make this discovery, however. Others have noticed Python’s rise in popularity in the past, including a report from Dice back in 2014.

Jeff Knupp a renowned Python developer seems to think that the growth is happening – and accelerating – simply because of one feature, Python’s Buffer Protocol. Knupp argues the low-level API offers zero-copy access to memory and large amounts of stored data, which lends itself well to data scientists. Why is that important? Well, because nearly everything is driven by big-data, analytics, intelligence, and machine learning these days. It makes sense why we’d see a focus on languages that not only make these processes easier, but more efficient.

Will that change anytime soon, though? Looking at the Stack Overflow trends report again, it’s easy to see that Python will be one of the most popular throughout 2018, as well.

How You Can Get Involved with Python?

Clearly, it’s time to get onboard if you’ve considered learning or working with Python in the past. The average salary of a Python developer, for instance, is climbing. Starting at about 85K in 2014 and jumping to $120K and beyond in silicon valley markets.

Start with our free beginner’s tutorial and coding guide. You’ll learn the benefits offered by the language, how you can set up and work with the environment, and how to work with frameworks like Django. It closes out by walking you through 6 projects, which you can complete in your free time, teaching you how to work with and shape the code.

From there, it’s up to you. We recommend checking out courses on Treehouse, of course. But you can go through any training or regimen you prefer. At the end of the free tutorial, you should have enough knowledge to decide where to go next, if not right into developing basic apps with the language.

Recommended Training Courses for Python Beginners

The post Hello Developers. Meet Python. The King of Growth appeared first on PythonForBeginners.com.

]]>
6753
DNS Lookup With Python https://www.pythonforbeginners.com/code-snippets-source-code/dns-lookup-python Sat, 19 Sep 2015 14:56:24 +0000 https://www.pythonforbeginners.com/?p=6744 The sockets module provides an easy way to look up a host name’s ip address. import socket addr1 = socket.gethostbyname('google.com') addr2 = socket.gethostbyname('yahoo.com') print(addr1, addr2) Which will output the following ip addresses: 173.194.121.9 98.138.253.109

The post DNS Lookup With Python appeared first on PythonForBeginners.com.

]]>
The sockets module provides an easy way to look up a host name’s ip address.


import socket

addr1 = socket.gethostbyname('google.com')
addr2 = socket.gethostbyname('yahoo.com')

print(addr1, addr2)

Which will output the following ip addresses:


173.194.121.9 98.138.253.109

The post DNS Lookup With Python appeared first on PythonForBeginners.com.

]]>
6744
Python String Concatenation and Formatting https://www.pythonforbeginners.com/concatenation/string-concatenation-and-formatting-in-python Sun, 09 Dec 2012 15:23:37 +0000 https://www.pythonforbeginners.com/?p=1866 One common task you’ll need to accomplish with any language involves merging or combining strings. This process is referred to as string concatenation. This post will describe string concatenation in Python. There are different ways to do that, and we will discuss the most common methods. After, we will explore formatting, and how it works. […]

The post Python String Concatenation and Formatting appeared first on PythonForBeginners.com.

]]>
One common task you’ll need to accomplish with any language involves merging or combining strings. This process is referred to as string concatenation. This post will describe string concatenation in Python. There are different ways to do that, and we will discuss the most common methods. After, we will explore formatting, and how it works.

What is String Concatenation in Python?

When we combine two or more strings in Python, the operation is called string concatenation.

In Python, there are many ways to concatenate or combine two or more strings. When we concatenate two or more strings, the new string is stored in a new string object. Obviously, this is because everything in Python is an object – which is why Python is an objected-oriented language.

In order to merge two strings into a single object, you may use the “+” operator. When writing code, that would look like this:

str1="Python"
str2="ForBeginners"
print("The first string is:",str1)
print("The second string is:",str2)
newStr=str1+str2
print("The concatenated string is:",newStr)

Output:

The first string is: Python
The second string is: ForBeginners
The concatenated string is: PythonForBeginners

In the above code, we have created two string objects str1 and str2. Then, we used the + operator to concatenate both strings. You can also concatenate more than two strings using the + operator as long as all the operands are valid strings. For instance, you can concatenate three strings in Python as shown below.

str1="Python"
str2="For"
str3="Beginners"
print("The first string is:",str1)
print("The second string is:",str2)
print("The third string is:",str3)
newStr=str1+str2+str3
print("The concatenated string is:",newStr)

Output:

The first string is: Python
The second string is: For
The third string is: Beginners
The concatenated string is: PythonForBeginners

Concatenate String and Integer in Python

Python doesn’t support concatenating a string and an integer. These are considered two separate types of objects. If you try to concatenate a string and an integer, the program will run into a Python TypeError exception as shown below.

myStr="PythonForBeginners"
myInt=1117
print("The string is:",myStr)
print("The integer is:",myInt)
newStr=myStr+myInt
print("The concatenated string is:",newStr)

Output:

The string is: PythonForBeginners
The integer is: 1117

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_56401/527270139.py in <module>
      3 print("The string is:",myStr)
      4 print("The integer is:",myInt)
----> 5 newStr=myStr+myInt
      6 print("The concatenated string is:",newStr)

TypeError: can only concatenate str (not "int") to str

In the above output, you can observe that we have tried to concatenate the string “PythonForBeginners” and the integer 1117. Due to this, the program runs into a TypeError exception.

If you still want to merge a string and an integer, you will need to convert the integer to a string. Then, you can perform string concatenation on the two Python strings as shown below.

myStr="PythonForBeginners"
myInt=1117
print("The string is:",myStr)
print("The integer is:",myInt)
newStr=myStr+str(myInt)
print("The concatenated string is:",newStr)

Output:

The string is: PythonForBeginners
The integer is: 1117
The concatenated string is: PythonForBeginners1117

In this example, we first converted the integer 1117 to a string using the str() function. Then, we concatenated the strings using the + operator. Hence, the program executes successfully and we get the output.

Multiply String By a Number in Python

When we multiply a string with an integer N, the string is repeated N times and we get a new string object. You can observe this in the following example.

myStr="PythonForBeginners"
N=3
print("The string is:",myStr)
print("The number is:",N)
newStr=myStr*N
print("The output string is:",newStr)

Output:

The string is: PythonForBeginners
The number is: 3
The output string is: PythonForBeginnersPythonForBeginnersPythonForBeginners

In the above code, we have multiplied the original string by 3. Hence, we get a new string with the original string repeated three times.

If you multiply a string by 0, you will get an empty string as shown below.

myStr="PythonForBeginners"
N=0
print("The string is:",myStr)
print("The number is:",N)
newStr=myStr*N
print("The output string is:",newStr)

Output:

The string is: PythonForBeginners
The number is: 0
The output string is: 

In the above code, we have multiplied the original string with 0. Hence, we get an empty string in the output. Even if you multiply a string with a negative integer, you will get an empty string in the output.

You cannot multiply a string with a floating point number. In this case, the program will run into a TypeError exception as shown in the following example.

myStr="PythonForBeginners"
N=3.5
print("The string is:",myStr)
print("The number is:",N)
newStr=myStr*N
print("The output string is:",newStr)

Output:

The string is: PythonForBeginners
The number is: 3.5

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_56401/1345002117.py in <module>
      3 print("The string is:",myStr)
      4 print("The number is:",N)
----> 5 newStr=myStr*N
      6 print("The output string is:",newStr)

TypeError: can't multiply sequence by non-int of type 'float'

In the above code, we have tried to multiply a string by 3.5. As 3.5 is a floating point number, you can observe that the program runs into a TypeError exception saying that we cannot multiply a sequence by a non-int type of float.

String Concatenation Using The join() Method

If you want to concatenate the strings separated by a string or character as a separator, you can use the join() method. The join() method, when invoked on a separator string, takes a list of strings as its input argument. After execution, it concatenates all the strings in the list by the separator and returns a new string. You can observe this in the following example.

str1="Python"
str2="For"
str3="Beginners"
print("The first string is:",str1)
print("The second string is:",str2)
print("The third string is:",str3)
newStr=" ".join([str1,str2,str3])
print("The concatenated string is:",newStr)

Output:

The first string is: Python
The second string is: For
The third string is: Beginners
The concatenated string is: Python For Beginners

In the above example, we have concatenated three strings using the space character. For this, we invoked the join() method on a string containing the space character and passed a list of all the input strings. After execution of the join() method, we get the desired output.

Suggested reading: String Manipulation

String Formatting in Python

In Python, we can format strings using string interpolation in three different ways.

String interpolation is a term used to describe the process of evaluating a string value that is contained as one or more placeholders. To put it simply, it helps developers with string formatting and concatenation. Hopefully, you are more familiar with the term yourself because it’s a crucial element of any programming language, especially Python.

We can perform string formatting using the following ways.

  • The % operator.
  • Using f-strings
  • The format() method

Let us discuss all three ways one by one.

Formatting Strings Using the % Operator in Python

The % operator uses format specifiers of values as placeholders for string formatting. When invoked on a string, the % operator takes a variable or tuple of variables as input and places the variables in the specified format specifiers in the string. To understand this, consider the following example.

x = "mangoes"
y = 15
z = "The number of %s in the basket is %d." % (x,y)
print(z)

Output:

The number of mangoes in the basket is 15.

In the above example, we have inserted an integer and a string value into the string “The number of %s in the basket is %d.” using the ‘% operator. Here, %s and %d is the format specifier for string and integer respectively. The other format specifiers are %f for float, %c for character, and %e for floating point exponential.

The variables in the tuple on the right side of the % operator contain values that are inserted into the strings. Here, the data type of the values in the variables should be in the same order as the data type defined by the format specifiers. Otherwise, the program will run into error.

x = "mangoes"
y = 15
z = "The number of %s in the basket is %d." % (y,x)
print(z)

Output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_6318/3859633640.py in <module>
      1 x = "mangoes"
      2 y = 15
----> 3 z = "The number of %s in the basket is %d." % (y,x)
      4 print(z)

TypeError: %d format: a real number is required, not str

In the above code, we have passed the integer value as the first element and the string value as the second element in the tuple passed to the % operator. In the string, the first format specifier expects a string and the second format specifier expects an integer. Due to this, the program runs into a TypeError exception.

In the above code, you also need to understand that the values passed in the tuple must be equal to the number of format specifiers in the string. Otherwise, the program will run into an error saying that there are not enough arguments for the format string. You can observe this in the following example.

x = "mangoes"
y = 15
z = "The number of %s in %d baskets is %d." % (x,y)
print(z)

Output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_6318/270842158.py in <module>
      1 x = "mangoes"
      2 y = 15
----> 3 z = "The number of %s in %d baskets is %d." % (x,y)
      4 print(z)

TypeError: not enough arguments for format string

In this example, there are three format specifiers in the string. However, we have passed only two values to the % operator. Hence, the program runs into error.

If we pass more input arguments than the format specifiers in the string, the program again runs into a TypeError exception saying not all arguments are converted during string formatting as shown below.

x = "mangoes"
y = 15
z = "The number of %s in %d baskets is %d." % (x,y,1117,"PFB")
print(z)

Output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_6318/120037793.py in <module>
      1 x = "mangoes"
      2 y = 15
----> 3 z = "The number of %s in %d baskets is %d." % (x,y,1117,"PFB")
      4 print(z)

TypeError: not all arguments converted during string formatting

In this example, there are three format specifiers in the string. However, we passed four values as input to the % operator. Hence, the program runs into error.

Format String Using f-strings in Python

As the name suggests, f-strings are used for formatting strings. In f-strings, you can directly pass the variable name inside curly braces. After execution of the statement, the values in the variable are interpolated in the actual string. You can observe this in the following example.

x = "mangoes"
y = 15
c=1117
z = f"The number of {x} in {y} baskets is {c}."
print(z)

Output:

The number of mangoes in 15 baskets is 1117.

Here, you can observe that we passed the variable names directly to the curly braces in the f-string. Using f-strings has the benefit that you don’t need to care about the order of variables passed to the strings as each variable is passed to the string using its name.

String Formatting Using the format() Method in Python

Instead of the % operator, you can use the format() method for string formatting in Python. In this case, we use curly braces {} instead of the format specifiers for placeholders. When we invoke the format() method on the string containing placeholders, we can pass the values as input arguments to the format() method. After execution of the statement, we get the formatted string. You can observe this in the following example.

x = "mangoes"
y = 15
z = "The number of {} in {} baskets is {}.".format(x,y,1117)
print(z)

Output:

The number of mangoes in 15 baskets is 1117.

While using the format() method for string formatting, if you pass more values to the format() method than the placeholders in the string, the program doesn’t run into an error. However, if you pass fewer values than the placeholders in the string. The program will run into errors.

One benefit of using the format() method is that you do not have to convert integers into a string before concatenating the data. It will do that automatically for you. This is one reason why it is the preferred operator method.

Another useful feature of the format() method is that you don’t actually have to feed the inputs to the interpreter in the same order that you want the variables to be displayed, as long as you specify the indices of the values in the placeholders. For example, you can put the position of the input values in the placeholders and pass the values to the format() method as shown below.

x = "mangoes"
y = 15
z = "The number of {0} in {2} baskets is {1}.".format(x,1117,y)
print(z)

Output:

The number of mangoes in 15 baskets is 1117.

In this output, you can observe that we have numbered the placeholders. Hence, the values are assigned to the placeholders according to the index number and not the actual order of the format specifiers in the string. The value 1117 is still assigned to the third placeholder even after giving it as a second input argument to the format() method. This is due to the reason that the third placeholder contains index 1.

Instead of the indices, you can also name the placeholders and pass the values as key-value pairs to the format() method as shown below.

x = "mangoes"
y = 15
z = "The number of {a} in {b} baskets is {c}.".format(a=x,c=1117,b=y)
print(z)

Output:

The number of mangoes in 15 baskets is 1117.

In this example, we named the placeholders a,b, and c. Then, we assign the values to the names while passing them to the format() method. You can observe that the values are assigned to the placeholders according to their names.

In this article, we discussed string concatenation and formatting in Python. To learn more about Python programming, you can read this article on Python Strings. You might also like this article on Reversing Lists and Strings.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

The post Python String Concatenation and Formatting appeared first on PythonForBeginners.com.

]]>
1866