0

so what I want to do is that I want to count the number if a string and be able to print for example:

str = 'string'
print(str.count(3))

and have return:

>> str or stri

something to be able to count or maybe split till a specific part of the string?

  • 2
    "what I want to do is that I want to count the number if a string" this sentence isnt really clear. Can you provide more details and clarity? – juanpa.arrivillaga Dec 17 '20 at 01:42
  • 2
    Note: Naming a variable `str` is a terrible idea; it shadows the built-in `str` constructor, preventing you from directly stringifying anything in the same scope (`str` specifically can be achieved in other ways, but it's a terrible idea to name-shadow built-ins in any event). – ShadowRanger Dec 17 '20 at 01:53
  • It looks like you are talking about **slicing**. See [Understanding slice notation](https://stackoverflow.com/q/509211/2745495) – Gino Mempin Dec 17 '20 at 02:49

1 Answers1

0

I don't understood yor question clearly but I am assuming you want to slice a string.

Syntax: string[index of first letter : index of last letter + 1]

for example in your case:

my_string = "string" 
print(my_string[0:4]) # or my_string[:4]

output :

str

REMEMBER INDEX STARTS FROM 0(ZERO) AND NOT TO USE SOME PREDEFINED WORDS IN PYTHON AS VARIABLE NAMES LIKE YOU USED str AS A VARIABLE NAME WHICH IS USED TO CHANGE DATA TYPE.

  • 1
    Guessing at answers to *wildly* underspecified questions is unhelpful; wait for the OP to actually ask an answerable question. – ShadowRanger Dec 17 '20 at 01:55