python basics(strings)

Save (0)
Close

Recommended

Description

practice questions on strings

 

Python Basics (Strings):

Strings are a set of characters enclosed in quotation marks. The string data type is one of the most commonly used data types in Python. String variables are created as soon as a value is assigned to them. The following are some of the basic operations with Strings:

1. Concatenation: Strings can be concatenated or joined using the “+” symbol.

For example,

a = ‘hello’
b = ‘world’
c = a + b
print(c)

Output: helloworld

2. Slicing: Python allows us to extract a specific part of a string, which is called string slicing. The slicing operation is performed using a colon(:).

For example,

a = ‘Python is Fun’
print(a[0:6])

Output: Python

3. Length of a String: We can find the length of a string using the built-in function, len().

For example,

a = ‘Welcome to Python’
print(len(a))

Output: 17

4. Lowercase and Uppercase: Python has built-in functions for converting strings to lowercase and uppercase.

For example,

a = ‘HELLO WORLD’
print(a.lower())

Output: hello world

a = ‘hello world’
print(a.upper())

Output: HELLO WORLD

5. String Replace: Python has a method for replacing a part of a string with another string.

For example,

a = ‘Hello, World!’
print(a.replace(‘H’, ‘J’))

Output: Jello, World!

6. String Split: Python has a built-in method for splitting strings into an array.

For example,

a = ‘Hello, World!’
print(a.split(‘,’))

Output: [‘Hello’, ‘ World!’]

In conclusion, strings are one of the fundamental data types in Python, and they have a plethora of operations that can be performed on them. As a programmer, it is important to master the basics of working with Strings, which will enable you to work with them effectively in your programs.