Featured Post

Python: Built-in Functions vs. For & If Loops – 5 Programs Explained

Image
Python’s built-in functions make coding fast and efficient. But understanding how they work under the hood is crucial to mastering Python. This post shows five Python tasks, each implemented in two ways: Using built-in functions Using for loops and if statements ✅ 1. Sum of a List ✅ Using Built-in Function: numbers = [ 10 , 20 , 30 , 40 ] total = sum (numbers) print ( "Sum:" , total) 🔁 Using For Loop: numbers = [ 10 , 20 , 30 , 40 ] total = 0 for num in numbers: total += num print ( "Sum:" , total) ✅ 2. Find Maximum Value ✅ Using Built-in Function: values = [ 3 , 18 , 7 , 24 , 11 ] maximum = max (values) print ( "Max:" , maximum) 🔁 Using For and If: values = [ 3 , 18 , 7 , 24 , 11 ] maximum = values[ 0 ] for val in values: if val > maximum: maximum = val print ( "Max:" , maximum) ✅ 3. Count Vowels in a String ✅ Using Built-ins: text = "hello world" vowel_count = sum ( 1 for ch in text if ch i...

2 Best Ways to Concatenate Strings in Python

Concatenation of strings in Python is possible by using the plus and join method. You can also do it in other ways. But in my point of view, these two are the best methods.


best ways concatenate string python

What are Strings?


A string represents in quotes. Let us see the data. It can have all kinds of data. A lot you can do by manipulating strings. Concatenation is one.


Example for strings 

Stringa='Uncle'
Stringb='is'
Stringc='Married'


Above example, you can see three strings. To make all these strings into a single, you need the concept of concatenation.

What is string concatenation?


String concatenation is a frequent activity in data science. Knowing this concept is helpful in your project and interviews as well.

You can do it using the two best methods. One is you can use plus operator. For the other one, you can use the join method.


#1: Using the plus operator


Stringa='Uncle'
Stringb='is'
Stringc='Married'
print(Stringa + ' ' + Stringb + ' ' + Stringc)


Result:

Uncle is Married


#2: Using the join method with separator.


Stringa='Uncle'
Stringb='is'
Stringc='Married'
a=' '.join([Stringa, Stringb, Stringc])
print(a)


Result:

Uncle is Married


Reference books


Reference links

Comments

Popular posts from this blog

SQL Query: 3 Methods for Calculating Cumulative SUM

5 SQL Queries That Popularly Used in Data Analysis

Big Data: Top Cloud Computing Interview Questions (1 of 4)