Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python substring

>>> x = "Hello World!"
>>> x[2:]
'llo World!'
>>> x[:2]
'He'
>>> x[:-2]
'Hello Worl'
>>> x[-2:]
'd!'
>>> x[2:-2]
'llo Worl'
Comment

python substring

# string [start:end:step]
string = "freeCodeCamp"
print(string[0:len(string)-1])		# freeCodeCam
print(string[0:5])		            # freeC
print(string[2:6])		            # eeCo
print(string[-1])		            # p
print(string[-5:])		            # eCamp
print(string[1:-4])                 # reeCode
print(string[-5:-2])	            # eCa
print(string[::2])		            # feCdCm
Comment

python get substring

my_string = "I love python."

# prints "love"
print(my_string[2:6])

# prints "love python."
print(my_string[2:])

# prints "I love python"
print(my_string[:-1])
Comment

Python substring

learn_coding = "You can learn to code for free! Yes, for free!"
substring = "paid"

print(learn_coding.index(substring))

# output

# Traceback (most recent call last):
#  File "main.py", line 4, in <module>
#    print(learn_coding.index(substring))
# ValueError: substring not found
Comment

PREVIOUS NEXT
Code Example
Python :: numpy concatenate arrays 
Python :: array slicing python 
Python :: get diagonals of 2d array 
Python :: counting unique values python 
Python :: python infinite l00p 
Python :: convert file dta in csv 
Python :: sort decreasing python 
Python :: django admin text box 
Python :: Merge 2 or more notebooks into one 
Python :: cursor python 
Python :: tensorflow use growing memory 
Python :: Python use number twice without variable 
Python :: python toupls 
Python :: dbscan python 
Python :: gene wilder pure imagination 
Python :: statsmodels fitted values 
Python :: python add new key to dictionary 
Python :: flask stream with data/context 
Python :: sklearn tree visualization 
Python :: python prevent print output 
Python :: state capitals python 
Python :: binary tree python 
Python :: how to set geometry to full screen in pyqt5 
Python :: how to get the top 100 frequent words on a python dataframe colummn 
Python :: websocket api python on close 
Python :: pandas split cell into multiple columns 
Python :: python terminal ui 
Python :: django group permissions method 
Python :: how to run test cases in python 
Python :: find the difference of strings in python 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =