diff --git a/IPython/core/inputtransformer2.py b/IPython/core/inputtransformer2.py
index 5777559fb..e2dd2d087 100644
--- a/IPython/core/inputtransformer2.py
+++ b/IPython/core/inputtransformer2.py
@@ -355,9 +355,14 @@ def find(cls, tokens_by_line):
         """Find the first escaped command (%foo, !foo, etc.) in the cell.
         """
         for line in tokens_by_line:
+            if not line:
+                continue
             ix = 0
-            while line[ix].type in {tokenize.INDENT, tokenize.DEDENT}:
+            ll = len(line)
+            while ll > ix and line[ix].type in {tokenize.INDENT, tokenize.DEDENT}:
                 ix += 1
+            if ix >= ll:
+                continue
             if line[ix].string in ESCAPE_SINGLES:
                 return cls(line[ix].start)
 

Cannot enter docstring in IPython
Type the following snippet and then hit `C-m` or `C-j` or `C-o` then IPython produces "Exception list index out of range" from `IPython/core/inputtransformer2.py`

```
In [1]: def f():
   ...:     """
```

Traceback:

```pytb
Unhandled exception in event loop:
  File "/home/takafumi/.virtualenvs/ipy7/lib/python3.7/site-packages/prompt_toolkit/eventloop/posix.py", line 154, in _run_task
    t()
  File "/home/takafumi/.virtualenvs/ipy7/lib/python3.7/site-packages/prompt_toolkit/eventloop/context.py", line 115, in new_func
    return func(*a, **kw)
  File "/home/takafumi/.virtualenvs/ipy7/lib/python3.7/site-packages/prompt_toolkit/application/application.py", line 548, in read_from_input
    self.key_processor.process_keys()
  File "/home/takafumi/.virtualenvs/ipy7/lib/python3.7/site-packages/prompt_toolkit/key_binding/key_processor.py", line 272, in process_keys
    self._process_coroutine.send(key_press)
  File "/home/takafumi/.virtualenvs/ipy7/lib/python3.7/site-packages/prompt_toolkit/key_binding/key_processor.py", line 179, in _process
    self._call_handler(matches[-1], key_sequence=buffer[:])
  File "/home/takafumi/.virtualenvs/ipy7/lib/python3.7/site-packages/prompt_toolkit/key_binding/key_processor.py", line 321, in _call_handler
    handler.call(event)
  File "/home/takafumi/.virtualenvs/ipy7/lib/python3.7/site-packages/prompt_toolkit/key_binding/key_bindings.py", line 78, in call
    return self.handler(event)
  File "/home/takafumi/repos/watch/ipython/IPython/terminal/shortcuts.py", line 109, in newline_or_execute
    status, indent = shell.check_complete(check_text)
  File "/home/takafumi/repos/watch/ipython/IPython/core/interactiveshell.py", line 3307, in check_complete
    status, nspaces = self.input_transformer_manager.check_complete(code)
  File "/home/takafumi/repos/watch/ipython/IPython/core/inputtransformer2.py", line 627, in check_complete
    lines = self.do_token_transforms(lines)
  File "/home/takafumi/repos/watch/ipython/IPython/core/inputtransformer2.py", line 551, in do_token_transforms
    changed, lines = self.do_one_token_transform(lines)
  File "/home/takafumi/repos/watch/ipython/IPython/core/inputtransformer2.py", line 534, in do_one_token_transform
    transformer = transformer_cls.find(tokens_by_line)
  File "/home/takafumi/repos/watch/ipython/IPython/core/inputtransformer2.py", line 359, in find
    while line[ix].type in {tokenize.INDENT, tokenize.DEDENT}:

Exception list index out of range
Press ENTER to continue...
```

Checked with current master eb8dac350abdabc350883ee72c12a151c6a0d138

diff --git a/IPython/core/tests/test_inputtransformer2.py b/IPython/core/tests/test_inputtransformer2.py
index 6a57b681c..d6c2fa3bd 100644
--- a/IPython/core/tests/test_inputtransformer2.py
+++ b/IPython/core/tests/test_inputtransformer2.py
@@ -233,6 +233,17 @@ def test_check_complete():
         for k in short:
             cc(c+k)
 
+def test_check_complete_II():
+    """
+    Test that multiple line strings are properly handled.
+
+    Separate test function for convenience
+
+    """
+    cc = ipt2.TransformerManager().check_complete
+    nt.assert_equal(cc('''def foo():\n    """'''), ('incomplete', 4))
+
+
 def test_null_cleanup_transformer():
     manager = ipt2.TransformerManager()
     manager.cleanup_transforms.insert(0, null_cleanup_transformer)

pip install -e .[test]
parse_log_pytest
requirements.txt
pytest
apt-get update apt-get install -y graphviz
3.9
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
attrs==25.3.0
backcall==0.2.0
certifi==2025.1.31
charset-normalizer==3.4.1
decorator==5.2.1
exceptiongroup==1.2.2
fastjsonschema==2.21.1
idna==3.10
importlib_metadata==8.6.1
iniconfig==2.1.0
ipykernel==5.5.6
-e git+https://github.com/ipython/ipython.git@8ed6e67ed9d7662222542d9f4c4bd92bd68d5adb#egg=ipython
ipython-genutils==0.2.0
jedi==0.19.2
jsonschema==4.23.0
jsonschema-specifications==2024.10.1
jupyter_client==8.6.3
jupyter_core==5.7.2
nbformat==5.10.4
nose==1.3.7
numpy==2.0.2
packaging==24.2
parso==0.8.4
pexpect==4.9.0
pickleshare==0.7.5
platformdirs==4.3.7
pluggy==1.5.0
prompt-toolkit==2.0.10
ptyprocess==0.7.0
Pygments==2.19.1
pytest==8.3.5
python-dateutil==2.9.0.post0
pyzmq==26.3.0
referencing==0.36.2
requests==2.32.3
rpds-py==0.24.0
simplegeneric==0.8.1
six==1.17.0
testpath==0.6.0
tomli==2.2.1
tornado==6.4.2
traitlets==5.14.3
typing_extensions==4.13.0
urllib3==2.3.0
wcwidth==0.2.13
zipp==3.21.0

name: ipython
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - pip=25.0=py39h06a4308_0
  - python=3.9.21=he870216_1
  - readline=8.2=h5eee18b_0
  - setuptools=75.8.0=py39h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tzdata=2025a=h04d1e81_0
  - wheel=0.45.1=py39h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - attrs==25.3.0
      - backcall==0.2.0
      - certifi==2025.1.31
      - charset-normalizer==3.4.1
      - decorator==5.2.1
      - exceptiongroup==1.2.2
      - fastjsonschema==2.21.1
      - idna==3.10
      - importlib-metadata==8.6.1
      - iniconfig==2.1.0
      - ipykernel==5.5.6
      - ipython-genutils==0.2.0
      - jedi==0.19.2
      - jsonschema==4.23.0
      - jsonschema-specifications==2024.10.1
      - jupyter-client==8.6.3
      - jupyter-core==5.7.2
      - nbformat==5.10.4
      - nose==1.3.7
      - numpy==2.0.2
      - packaging==24.2
      - parso==0.8.4
      - pexpect==4.9.0
      - pickleshare==0.7.5
      - platformdirs==4.3.7
      - pluggy==1.5.0
      - prompt-toolkit==2.0.10
      - ptyprocess==0.7.0
      - pygments==2.19.1
      - pytest==8.3.5
      - python-dateutil==2.9.0.post0
      - pyzmq==26.3.0
      - referencing==0.36.2
      - requests==2.32.3
      - rpds-py==0.24.0
      - simplegeneric==0.8.1
      - six==1.17.0
      - testpath==0.6.0
      - tomli==2.2.1
      - tornado==6.4.2
      - traitlets==5.14.3
      - typing-extensions==4.13.0
      - urllib3==2.3.0
      - wcwidth==0.2.13
      - zipp==3.21.0
prefix: /opt/conda/envs/ipython

IPython/core/tests/test_inputtransformer2.py::test_check_complete_II
IPython/core/tests/test_inputtransformer2.py::test_continued_line IPython/core/tests/test_inputtransformer2.py::test_find_assign_magic IPython/core/tests/test_inputtransformer2.py::test_transform_assign_magic IPython/core/tests/test_inputtransformer2.py::test_find_assign_system IPython/core/tests/test_inputtransformer2.py::test_transform_assign_system IPython/core/tests/test_inputtransformer2.py::test_find_magic_escape IPython/core/tests/test_inputtransformer2.py::test_transform_magic_escape IPython/core/tests/test_inputtransformer2.py::test_find_autocalls IPython/core/tests/test_inputtransformer2.py::test_transform_autocall IPython/core/tests/test_inputtransformer2.py::test_find_help IPython/core/tests/test_inputtransformer2.py::test_transform_help IPython/core/tests/test_inputtransformer2.py::test_check_complete IPython/core/tests/test_inputtransformer2.py::test_null_cleanup_transformer
IPython/core/inputtransformer2.py
IPython/core/inputtransformer2.py