How to add single quotes, double quotes, or both quotes in the python strings?
Example:
course = "Python's for everyone" print(course) new_course = 'Python for "EveryOne"' print(new_course) email = ''' Hi John!, Welcome to helperscript to learn Python's for "Everyone" Thank you, Support Team ''' print(email)
Triple quotes (”’Python for “Everyone””’) – Three single quotes from the start and three single quotes from the end. This type of string escapes all the symbols in the string.
Split string into an array (or) Split the string in python
Python string starts index with zero(0).
Example: course variable has string “Python for Everyone”
Course = ‘Python for Everyone’
Get the first character of a string in python
course[0]
Get the second character of a string in python
course[1]
Get the last character of a string in python
course[-1]
Get the last second character of a string in python
course[-2]
Get the first three character of a string in python
course[0:3]
Remove the first character and remove the last 3 characters of a string in python
course[1:-3]
Remove the character after 6 index
course[:6]
Remove the first 6 characters in the string
course[6:]
Clone or copy the string
course[:]
Code:
# strings course = 'Python for Everyone' print(course); print('course[0] = ' + course[0]) print('course[1] = ' + course[1]) print('course[-1] = ' + course[-1]) print('course[-2] = ' + course[-2]) print('course[0:3] = ' + course[0:3]) print('course[1:-3] = ' + course[1:-3]) print('course[:5] = ' + course[:6]) print('course[6:] = ' + course[6:]) print('course[:] = ' + course[:])
OUTPUT:
String Concatenation
Two ways to concat the string dynamically.
Normal way – firstname +’ ’+ lastname +’ is a coder’
Format way – f‘{firstname} {lastname} is a code’
f – prefix with f define a formatted string
Code:
Python strings method
Let,
course = ‘Python for Beginners’
# find the length of string
len(course)
# convert uppercase
course.upper()
# convert lowercase
course.lower()
# convert first character uppercase in each word
course.title()
# find an index of character or sequence of character.
Python find() method is case sensitive.
print(course.find(‘P’)) # gives index of capital P
print(course.find(‘p’)) # return -1 if not present.
print(course.find(‘Beginners’))
# Replace a character
course.replace(‘P’, ‘J’)
# Replace a sequence of character
course.replace(‘Python’, ‘Angular’)
# find a string contains in the existing string(case sensitive)
‘Python’ in course
App.py
course = 'Python for Beginners' # find the length of string print(len(course)) # convert uppercase print(course.upper()) # convert lowercase print(course.lower()) # convert first character uppercase in each word print(course.title()) # find index of character or sequence of character print(course.find('P')) # capital print(course.find('p')) # small letter print(course.find('Beginners')) # Replace a character print(course.replace('P', 'J')) # Replace a sequence of character print(course.replace('Python', 'Angular')) # find a string contains in existing string print('Python' in course) # gives boolean output either True or False
In Next article, we see about Operators in Python.
Some genuinely nice and utilitarian info on this site, too I think the design and style has wonderful features. Joleen Ezechiel Broderick
This is a great tip especially to those fresh to the blogosphere. Ileana Isidore Stafford
Very good article. I will be experiencing many of these issues as well.. Riki Farrel Cho