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
[Link] 1/6
7/27/24, 8:54 PM Revolutionizing the Job Market | NxtWave
File "[Link]", 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 * * *
[Link] 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)
[Link] 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.
[Link] 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
[Link] 5/6
7/27/24, 8:54 PM Revolutionizing the Job Market | NxtWave
IndexError: string index out of range
[Link] 6/6