python4
python4
String Concatenation
Code
PYTHON
Output
Hello World
Concatenation Errors
Code
PYTHON
1 a = "*" + 10
2 print(a)
Output
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=56e26399-b147-4b8b-bb2c-d4f390c2a513&s_id=2689146e… 1/6
7/27/24, 8:54 PM Revolutionizing the Job Market | NxtWave
String Repetition
Code
PYTHON
1 a = "*" * 10
2 print(a)
Output
**********
Code
PYTHON
1 s = "Python"
2 s = ("* " * 3) + s + (" *" * 3)
3 print(s)
Output
* * * Python * * *
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=56e26399-b147-4b8b-bb2c-d4f390c2a513&s_id=2689146e… 2/6
7/27/24, 8:54 PM Revolutionizing the Job Market | NxtWave
Length of String
len() returns the number of characters in a given string.
Code
PYTHON
1 username = input()
2 length = len(username)
3 print(length)
Input
Ravi
Output
Example - 1:
Code
PYTHON
1 username = input()
2 print(username)
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=56e26399-b147-4b8b-bb2c-d4f390c2a513&s_id=2689146e… 3/6
7/27/24, 8:54 PM Revolutionizing the Job Market | NxtWave
Input
Ajay
Output
Ajay
Example - 2:
Code
PYTHON
1 username = input()
2 age = input()
3 print(username + " is " + age + " years old")
Input
Ravi
10
Output
Note
Even though you can't directly combine strings and integers, the code works
because input() returns a string.
input() converts user-entered data (including numbers, booleans, etc) to string. You
will learn more about this in further sessions.
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=56e26399-b147-4b8b-bb2c-d4f390c2a513&s_id=2689146e… 4/6
7/27/24, 8:54 PM Revolutionizing the Job Market | NxtWave
In this case, "10" becomes a string, allowing it to Concatenate and be the part of the
final message.
String Indexing
We can access an individual character in a string using their positions (which start from 0).
These positions are also called as index.
Code
PYTHON
1 username = "Ravi"
2 first_letter = username[0]
3 print(first_letter)
Output
IndexError
Code
PYTHON
1 username = "Ravi"
2 print(username[4])
Output
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=56e26399-b147-4b8b-bb2c-d4f390c2a513&s_id=2689146e… 5/6
7/27/24, 8:54 PM Revolutionizing the Job Market | NxtWave
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=56e26399-b147-4b8b-bb2c-d4f390c2a513&s_id=2689146e… 6/6