Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output first 100 characters in a string

Tags:

python

Can seem to find a substring function in python.

Say I want to output the first 100 characters in a string, how can I do this?

I want to do it safely also, meaning if the string is 50 characters it shouldn't fail.

like image 210
Blankman Avatar asked Aug 15 '10 05:08

Blankman


People also ask

How do I print the first 100 characters of a string in Python?

Another approach is to use 'yourstring'. ljust(100)[:100]. strip() . This will give you first 100 chars.

How do I find the first 10 characters of a string?

How to find the first 10 characters of a string in C#? To get the first 10 characters, use the substring() method. string res = str. Substring(0, 10);

How do I print the first n characters of a string?

Just %s will work in the case, you are not entering a string with 'spaces'. Also, In case you want to print the N number of characters, you need to put len<N or len<=N-1 . Just, put inp instead of &inp .

How do you extract the first 10 characters of a string in Python?

To access the first n characters of a string in Python, we can use the subscript syntax [ ] by passing 0:n as an arguments to it. 0 is the starting position of an index. n is the number of characters we need to extract from the starting position (n is excluded).


1 Answers

print my_string[0:100] 
like image 109
icktoofay Avatar answered Sep 17 '22 23:09

icktoofay