How to get the first N words from a string in Python

1 Answer

0 votes
s = "python c c++ java c#"

N = 3
lst = s.split()[:N]

first3 = ' '.join(lst)

print(first3)




'''
run:

python c c++

'''

 



answered Feb 8, 2022 by avibootz

Related questions

1 answer 98 views
1 answer 128 views
2 answers 168 views
2 answers 139 views
2 answers 150 views
1 answer 107 views
...