Question

Write about strings and input in python. Explain with 2 examples.

Write about strings and input in python. Explain with 2 examples.

Homework Answers

Answer #1

String in Python:

A string is a sequence of characters.In Python, a string is a sequence of Unicode characters.

creating a string in Python:
Strings can be created by enclosing characters inside a single quote or double-quotes.

Example:
# defining strings in Python

my_string = 'Hello'
print(my_string)
>> Hello

my_string = "Hello"
print(my_string)
>> Hello

Access characters in a string:
We can access individual characters using indexing and a range of characters using slicing.
The index of -1 refers to the last item, -2 to the second last item and so on. We can access a range of items in a string by using the slicing operator (:).

Example:

#Accessing string characters in Python
str = 'AGGREGATE ' ''
print('str = ', str)

#first character
print('str[0] = ', str[0])
>> A

#last character
print('str[-1] = ', str[-1])
>> E

#slicing 2nd to 5th character
print('str[1:5] = ', str[1:5])
>> GGRE

#slicing 6th to 2nd last character
print('str[5:-2] = ', str[5:-2])
>> GA

String Operations:
Concatenation of Two or More Strings :
Joining of two or more strings into a single one is called Concatenation.

The + operator does this in Python. Simply writing two string literals together also concatenates them.
The * operator can be used to repeat the string for a given number of times.

Example:

# Python String Operations
str1 = 'Hello'
str2 ='World!'

# using +
print('str1 + str2 = ', str1 + str2)
>> HelloWorld!

# using *
print('str1 * 3 =', str1 * 3)
>> HelloHelloHello

Iterating Through a string:
Iterate through a string using a for loop:

Example:

# Iterating through a string
count = 0
for letter in 'Hello World':
if(letter == 'l'):
count += 1
print(count,'letters found')
>> 3 Letters found

Python Input:

In Python, we have the input() function to allow this. The syntax for input() is:

input([prompt])


where prompt is the string we wish to display on the screen. It is optional.

Example:

name = input('What is your name? ')
What is your name? chandu
>>> name
'chandu'

>>> num = input('Enter a number: ')
Enter a number: 10
>>> num
'10'


Here, we can see that the entered value 10 is a string, not a number. To convert this into a number we can use int() or float() functions.

>>> int('10')
10
>>> float('10')
10.0

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
"Python strings are immutable" Explain in details what is that mean. Provide examples of strings in...
"Python strings are immutable" Explain in details what is that mean. Provide examples of strings in Python and how it can be used
asap python: Write a function called fileCaseSwap that takes 2 filenames (strings) as input parameters. The...
asap python: Write a function called fileCaseSwap that takes 2 filenames (strings) as input parameters. The function should: Open the first file for reading Open the second file for writitng Read the first file Swap the case (swapcase) of every character in the file Write the replaced text to the second file. test call: fileCaseSwap("emma-short.txt", "emma-short-swap.txt") fileCaseSwap("emma.txt", "emma-swap.txt") """ """ The first 3 lines of the emma-short-swap.txt file should be emma bY jANE aUSTEN
Provide explanation about Iterations (python). Give examples and write a program.
Provide explanation about Iterations (python). Give examples and write a program.
Write a Python function first_chars that takes one parameter, which is a nested list of strings....
Write a Python function first_chars that takes one parameter, which is a nested list of strings. It returns a string containing the first character of all of the non-empty strings (at whatever depth they occur in the nested list) concatenated together. Here are a couple of examples of how the function should behave when you're done: >>> first_chars(['Boo', 'is', 'happy', 'today']) 'Biht' >>>first_chars(['boo', 'was', ['sleeping', 'deeply'], 'that', 'night', ['as', ['usual']]]) 'bwsdtnau'
Write a Python program to ask user input of a string, then ask user input of...
Write a Python program to ask user input of a string, then ask user input of what letters they want to remove from the string. User can either put in "odd", "even" or a number "n" that is greater than 2. When user input "odd", it will remove the characters which have odd numbers in the sequence of the string. When user input "even", it will remove the characters which have even numbers in the sequence of the string. When...
For each of the following Python programs P and input strings I, give the output P(I),...
For each of the following Python programs P and input strings I, give the output P(I), using the formal definition of P(I) given, and employing any reasonable reference computer C: Definition of P(I), the output of a Python program. Let P be a Python program with respect to a reference computer system C. Suppose P has main function M, and let I be a string of ASCII characters that will be used as input to P. The output of P...
Use Python: # Problem Set 03: - Write a program to input an uncertain string *S*...
Use Python: # Problem Set 03: - Write a program to input an uncertain string *S* of words and space/tab - Remove space/tab in S - Remove identical words - Sort all words in the order of a-z - Print the output strings - Example: - Input String: "hello world and practice makes perfect and hello world again" - Output String: "again and hello makes perfect practice world" - Tips: - use *set* to remove identical words, - use *sorted()*...
using Python: Write a function named safe input(prompt,type) that works like the Python input function, except...
using Python: Write a function named safe input(prompt,type) that works like the Python input function, except that it only accepts the specified type of input. The function takes two arguments: r prompt: str r type: int, float, str The function will keep prompting for input until correct input of the specified type is entered. The function returns the input. If the input was specified to be a number ( float or int), the value returned will be of the correct...
Develop an algorithm in Python and write a code that prompts the user to input a...
Develop an algorithm in Python and write a code that prompts the user to input a number and returns the square root of that number.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT