Python String Concatenation | Python Find String in List

Python String Concatenation | Python Find String in List

In this lecture, we will discuss “Python String Concatenation | Python Find String in List”. If you are a new visitor to my website, you can also read my previous lecture that is written about string in details. Are you know about string? ooh, that’s not a problem.

First, we are going to learn about string in python. Then we will discuss how to concatenate with other data types. Also, You can learn how to print a specific word in a given string.

[mks_pullquote align=”left” width=”848″ size=”15″ bg_color=”#ecf7b9″ txt_color=”#282828″]

Python String

A string value is written in double quotation (” “). Single quotes work will be the same as double quotes in Python.

Example: 1
 name = "ABDUL JABBAR"
[/mks_pullquote]

Python String Concatenation

Python String Concatenation Int to String

Example: 2
name = ("my name is "+2)
print (name)

Output:  Error will occur. Because string cannot be concatenated with Int Data Types.
[mks_pullquote align=”left” width=”848″ size=”16″ bg_color=”#ddb8b8″ txt_color=”#161616″]Note: There is may the following error occurs “string cannot be concatenated with Int Data Types”. You need to convert int to str to concatenate and get the required output. As you see in the following program that is given below.[/mks_pullquote]

Example:3

name = ("my name is "+ "2")
print (name)

Output:  my name is 2.

The correct answer, because the string can be concatenated with other string because Data types are same of both values.

Concatenate int to string with multiplication Sign

Example:4

name = ("my name is"*2)
print (name)

Output: my name is my name is

When we multiply any string with an integer that string’s alphabets will be repeated. You can see above the program’s Output “my name is” string repeated 2 times.

Python Find String in List

Print any specific word or alphabets in a given string?

It is much important and useful and widely used in a programming concept. Google also used this concept.

Example:5

str = "abcabcabcabcabc"   # if we want to print ‘b’ that is at a second position in the given string.
print (str[1])

Output: b

Example:6

str = "friend"       #we will access “i” keyword from the following statements
print (str[3])        # e keyword at fourth position we can acess through 3 because index start from 0.

Output:  e

Because the index of the string will start from 0. And “b” in the second position in the given string then we can write the following above statements to access this specific alphabet.

Example:7

str = "friend"   
print (str[8])     # because 8 is out of range, does not exist in any alphabet.

Output:  string index out of range

In the last example, you can see the string index is out of range. Because any alphabet does not exist at a given index in the above example. Indexing starts from 0. when the index of the string does not exist then your answer will be out of range.


[mks_pullquote align=”left” width=”848″ size=”15″ bg_color=”#dda44d” txt_color=”#0a0a0a”]I hope this article is very helpful to understand “Python String Concatenation | Python Find String in List”. If you have any question then you can discuss in the comments section. Please also give your feedback. You can also learn HTML5, PHP7, Bootstrap4.0 and many other programming languages. Thank you!.[/mks_pullquote]

 

Add a Comment

Your email address will not be published. Required fields are marked *