0% found this document useful (0 votes)
6 views5 pages

5 Type Conversation

The document provides an overview of string slicing and type conversion in Python, including examples of how to slice strings and convert between data types. It explains the use of functions like int(), float(), and str() for type conversion, along with potential errors that can arise from invalid conversions. Additionally, it includes code snippets demonstrating these concepts and their outputs.

Uploaded by

kalyan 16-527
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

5 Type Conversation

The document provides an overview of string slicing and type conversion in Python, including examples of how to slice strings and convert between data types. It explains the use of functions like int(), float(), and str() for type conversion, along with potential errors that can arise from invalid conversions. Additionally, it includes code snippets demonstrating these concepts and their outputs.

Uploaded by

kalyan 16-527
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

12/13/24, 2:34 AM Revolutionizing the Job Market | NxtWave

Type Conversion

String Slicing

Obtaining a part of a string is called string slicing.

Code
PYTHON

1 variable_name[start_index:end_index]

Start from the start_index and stops at end_index

end_index is not included in the slice.

Code
PYTHON

1 message = "Hi Ravi"


2 part = message[3:7]
3 print(part)

Output

Ravi

Slicing to End

If end index is not specified, slicing stops at the end of the string.

Code
PYTHON

1 message = "Hi Ravi"


2 part = message[3:]
3 print(part)

Output

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=56e26399-b147-4b8b-bb2c-d4f390c2a513&s_id=3d05cc08-d44… 1/5
12/13/24, 2:34 AM Revolutionizing the Job Market | NxtWave

Ravi

Slicing from Start

If start index is not specified, slicing starts from the index 0.

Code
PYTHON

1 message = "Hi Ravi"


2 part = message[:2]
3 print(part)

Output

Hi

Checking Data Type

Check the datatype of the variable or value using

type()

Printing Data Type

Code
PYTHON

1 print(type(10))
2 print(type(4.2))
3 print(type("Hi"))

Output

<class 'int'>
<class 'float'>
<class 'str'>

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=56e26399-b147-4b8b-bb2c-d4f390c2a513&s_id=3d05cc08-d44… 2/5
12/13/24, 2:34 AM Revolutionizing the Job Market | NxtWave

Type Conversion

Converting the value of one data type to another data type is called Type Conversion or Type Casting.
We can convert

String to Integer

Integer to Float

Float to String and so on.

String to Integer

int() converts valid data of any type to integer

Code
PYTHON

1 a = "5"
2 a = int(a)
3 print(type(a))
4 print(a)

Output

<class 'int'>
5

Invalid Integer Conversion

Code
PYTHON

1 a = "Five"
2 a = int(a)
3 print(type(a))

Output

ValueError:
invalid literal for int() with base 10: 'Five'

Code
PYTHON

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=56e26399-b147-4b8b-bb2c-d4f390c2a513&s_id=3d05cc08-d44… 3/5
12/13/24, 2:34 AM Revolutionizing the Job Market | NxtWave
1 a = "5.0"
2 a = int(a)
3 print(type(a))

Output

invalid literal for int() with base 10: '5.0'

Adding Two Numbers

Code
PYTHON

1 a = input()
2 a = int(a)
3 b = input()
4 b = int(b)
5 result = a + b
6 print(result)

Input

2
3

Output

Integer to String

str() converts data of any type to a string.

Code
PYTHON

1 a =
input()
2 a =
int(a)
3 b =
input()
4 b =
int(b)
l b
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=56e26399-b147-4b8b-bb2c-d4f390c2a513&s_id=3d05cc08-d44… 4/5
12/13/24, 2:34 AM Revolutionizing the Job Market | NxtWave
5 result = a + b
6 print("Sum: " + str(result))

Input

2
3

Output

Sum: 5

Summary
1. int() -> Converts to integer data type

2. float() -> Converts to float data type


3. str() -> Converts to string data type
4. bool() -> Converts to boolean data type

Submit Feedback

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

You might also like