0% found this document useful (0 votes)
3 views

python4

The document provides an overview of basic string operations in Python, including string concatenation, repetition, and indexing. It explains how to concatenate strings, the use of the len() function to determine string length, and how to take user input. Additionally, it highlights common errors such as TypeError and IndexError when performing string operations.

Uploaded by

Prachi More
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

python4

The document provides an overview of basic string operations in Python, including string concatenation, repetition, and indexing. It explains how to concatenate strings, the use of the len() function to determine string length, and how to take user input. Additionally, it highlights common errors such as TypeError and IndexError when performing string operations.

Uploaded by

Prachi More
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

7/27/24, 8:54 PM Revolutionizing the Job Market | NxtWave

Inputs and Outputs Basics

Working with Strings

String Concatenation

Joining strings together is called string concatenation.

Code
PYTHON

1 a = "Hello" + " " + "World"


2 print(a)

Output

Hello World

Concatenation Errors

String Concatenation is possible only with strings.

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

File "main.py", line 1


a = "*" + 10
^
TypeError:
can only concatenate str (not "int") to str

String Repetition

* operator is used for repeating strings any number of times as required.

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

Take Input From User

input() allows flexibility to take the input from the user.

input() reads a line of input as a string.

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

Ravi is 10 years old

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

Attempting to use an index that is too large will result in an error:

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

IndexError: string index out of range

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=56e26399-b147-4b8b-bb2c-d4f390c2a513&s_id=2689146e… 6/6

You might also like